Search code examples
angularangular-reactive-forms

How to get a formGroup valid after clearValidators


I'm trying to remove a Validators.required in order to make a form valid. But whatever I do, none of that is happening

Here is my logic:

form = new FormGroup({
    name: new FormControl('', [Validators.required]),
    type: new FormControl('test', [Validators.required]),
});

this.form.get('name').clearValidators();
this.form.updateValueAndValidity();
this.cdr.detectChanges(); 

DEMO

Whatever I do, the form.valid is always false. Any suggestions why the form.valid property is not true?


Solution

  • You need to call updateValueAndValidity on individual control

    this.form.get('name').clearValidators();
    this.form.get('name').updateValueAndValidity();
    

    Forked Working Example: