Search code examples
angularangular-reactive-formsangular2-form-validation

Dynamic Form validation is not working in Angular 6


Scenario: User want to retrieve email or reset password. Person will select respective option as per requirement. Submit button will get enable only when form is valid.

Problem: I want to dynamically update validation according to selected option. But it is not working. Definitely I'm missing something. I used clearValidators, but it is also not working. Here is the working code.

For testing use: email : [email protected] password: Test@1234


Solution

  • Try updating the individual controls value and validity. For example

    this.loginTroubleForm.get('email').updateValueAndValidity();
    

    Do this for each control for which you are setting the validator to null.