8

I found this post but it cant solve my problem. I can't validate a simple form with FormControls disabled. I did make a stackblitz example here, please check out my code and verify that is impossible to verify the value of the name control if it's disabled.

Stackblitz code

Thank you in advance.

Ricky
  • 1,662
  • 3
  • 24
  • 38
  • Possible duplicate of [Disable Input fields in reactive form](https://stackoverflow.com/questions/42840136/disable-input-fields-in-reactive-form) – Christian Benseler Feb 04 '19 at 19:34
  • Check the latest answer from this post: https://stackoverflow.com/questions/42840136/disable-input-fields-in-reactive-form when a form control is disabled, it is exempted from validation checks. – Christian Benseler Feb 04 '19 at 19:34
  • 3
    If you want not loose the validate you can use [attr.disabled]="true" (or any condition). This give your control an apparence of disabled but still is "validating" – Eliseo Feb 04 '19 at 19:45
  • Thank you to all for your responses, specially to you @Eliseo, your solution works for me. – Ricky Feb 04 '19 at 20:14

2 Answers2

13

Solution by @Eliseo:

If you want not loose the validate you can use [attr.disabled]="condition ? true : null" (or any condition). This give your control an apparence of disabled but still is "validating".

Anil Kumar
  • 65
  • 5
Ricky
  • 1,662
  • 3
  • 24
  • 38
4

The reason disabled fields are not validated is because angular forms skips them from the validation. Since the control level is skipped, you will need to add that validation on the group level. credit goes to this comment https://github.com/angular/angular/issues/25182#issuecomment-408629027

The code would look like this:

this.formGroup.setValidators((form) => Validators.required(form.get('disabledControl'));
Dharman
  • 26,923
  • 21
  • 73
  • 125
Rstar37
  • 289
  • 2
  • 5