Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set form manually to invalid angular 2+

Tags:

Is there a way to set a formGroup to invalid state manually?

I tried myForm.invalid = true; and also myForm.status = 'INVALID'

But invalid and status are const.

I dont want to mark a specific control as invalid. But the whole form.

like image 773
AngularDebutant Avatar asked Sep 29 '17 11:09

AngularDebutant


People also ask

How do you set a dirty form?

$setDirty(); Sets the form to a dirty state. This method can be called to add the 'ng-dirty' class and set the form to a dirty state (ng-dirty class). This method will also propagate to parent forms.

How do you add validation in reactive form?

In a reactive form, the source of truth is the component class. Instead of adding validators through attributes in the template, you add validator functions directly to the form control model in the component class. Angular then calls these functions whenever the value of the control changes.


1 Answers

Try this:

myForm.setErrors({ 'invalid': true }); 
like image 194
tltjr Avatar answered Oct 10 '22 17:10

tltjr