Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 FormGroup Add Validators dynamic

i'm tring to add validator to FormContol dynamic (not on initialization) and it's not work....

the code:

this.formGroup.controls["firstName"].validator = Validators.Required;

Did anyone do it?

like image 274
haya Avatar asked Apr 25 '17 06:04

haya


People also ask

How do you set a validator dynamically?

We can add Validators dynamically using the SetValidators or SetAsyncValidators. This method is available to FormControl, FormGroup & FormArray. There are many use cases where it is required to add/remove validators dynamically to a FormControl or FormGroup.

Which method is used to add dynamic validation to the forms in Angular?

Dynamically Add Validators We need to listen to optionB value changes and based on that we add or remove the validators we require. We also call the control's updateValueAndValidity() method, as we need to recalculate the value and validation status of the control.


2 Answers

Try this, it should work

this.formGroup.controls["firstName"].setValidators(Validators.required);

For multiple validators

this.formGroup.controls["firstName"].setValidators([Validators.required, Validators.minLength(2)]);

But doing so will override any validators that are provided during initialization

EDIT :

To reflect the form controls with newly added Validators immediately, we have to call this.formGroup.controls["firstName"].updateValueAndValidity(); after setting validators dynamically.

this.formGroup.controls["firstName"].setValidators(Validators.required);
this.formGroup.controls["firstName"].updateValueAndValidity();

DEMO for the same

* NOTE *

updateValueAndValidity() will trigger a valueChanges event even if the value didn't really change (potentially resulting in an infinite loop, if you're calling it from within a valueChanges subscription). You can prevent that by using the optional parameter object: { onlySelf: true, emitEvent: false}

like image 152
Amit Chigadani Avatar answered Sep 26 '22 21:09

Amit Chigadani


While Amit Chigadani's answer is correct, please bear in mind the following.

While this solution works for updating validators, the new validators themselves will not be run, until you change the form fields value. it is thus recommended to run the following function straight after you update you validators ;)

In laymans terms: Add the line, If you want your ng-valid and ng-invalid classes to update themselves.

this.formGroup.controls["firstName"].setValidators([Validators.required, Validators.minLength(2)]);
this.formGroup.controls["firstName"].updateValueAndValidity();

In case you only want to update the value and validity of this single control, then add this option

this.formGroup.controls["firstName"].setValidators([Validators.required, Validators.minLength(2)]);
this.formGroup.controls["firstName"].updateValueAndValidity({onlySelf: true});

Another sidenote. In Angular2, one shouldn't use the array syntax to get form group controls, but this

this.formGroup.get("firstName");

Which also accepts the dot notation

this.formGroup.get("userSectionFormGroup.firstName");
like image 38
Karl Johan Vallner Avatar answered Sep 22 '22 21:09

Karl Johan Vallner