Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 Required only one field from many fields Reactive Form

I am new in angular. I have one scenario where I need only one field required from 5 fields in the form, means if the user fills at least one field then form makes valid.

Thanks in advance.

like image 533
user3492620 Avatar asked Nov 17 '22 20:11

user3492620


1 Answers

Since you need to check for the validity of whole form only if one of the fields is non empty , You can manually set the validity like below :

if(!this.valid){
    this.form.setErrors({ 'invalid': true});
}else{
    this.form.setErrors(null);
}

Where this.valid is your condition based on which you can set the validity

You can check the example : https://angular-exmphk.stackblitz.io

You can also check the answer : FormGroup validation in "exclusive or" which does form validation based on some condition

Hope this helps

like image 71
CruelEngine Avatar answered Nov 19 '22 09:11

CruelEngine