Home > OS >  How to make check validation for each controls syntax less repetitve
How to make check validation for each controls syntax less repetitve

Time:09-06

I have a form-group that contains 7 form-controls, if any of the form controls is invalid, I shall return a false disable the submit button.

This is my code

checkFormValidation () : boolean {
    var valid = true ;
    if(this.new.controls..invalid){
      return false
    }
    if(this.new.controls..invalid){
      return false
    }
    if(this.newm.controls..invalid){
      return false
    }    
    ....
  }

This is highly repetitive and I would like to ask if there are any way to make this code shorter and less repeitive?

CodePudding user response:

FormControl can be a child of FormGroup or FormArray.

If you use FormBuilder or correctly create the parent, then the parent holds the validity of all fields.

const group = this.formBuilder.group({
  username: ['', [Validators.required]],
  password: ['', [Validators.required]],
});

console.log(group.valid); // false
  • Related