Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I validate reactive form controls programmatically?

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;
}
like image 799
Joba Avatar asked Oct 18 '18 13:10

Joba


People also ask

How do I validate FormArray controls?

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.

How do you add a validator to FormControl 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.


1 Answers

You can programmatically trigger the validator using the following.

this.formGroup.controls['controlNameHere'].markAsTouched();
like image 160
Marshal Avatar answered Oct 04 '22 08:10

Marshal