Issue
one of my controls is a bool and if that control is true i want the two of my another controls to be required else they can be null.
const controls = [
{name: "bool", control: new FormControl(''), validators: []},
{name: "firstName", control: new FormControl(''), validators: ???},
{name: "lastName", control: new FormControl(''), validators: ???},
Solution
You want to cross validate: https://angular.io/guide/form-validation#adding-cross-validation-to-reactive-forms
Or use regex like:
controls.get('bool').value === true ? Validators.required : null
Or set them programmatically
if (controls.get('bool').value === true) {
controls['firstName'].setValidators([Validators.required])
}
Answered By - Joosep Parts
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.