I have a FormGroup
object of my reactive form. Now I want to trigger the form validation programmatically.
I already check the form with this code, but my control css status classes aren't set, like it's done when I click the control and click outside the control.
if (!this.formGroup.valid) {
// TODO: Fix that invalid controls don't get highlighted
return;
}
Validating Angular FormArray First you need to add the required validators while creating a new product form group inside the addProduct method. Now let's add a span element adjacent to the input control. Add the following CSS to the app. component.
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.
You can programmatically trigger the validator using the following.
this.formGroup.controls['controlNameHere'].markAsTouched();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With