Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force ng-dirty in angularjs form validation

Doing form validation with angularjs I want to mark all required fields as erroneous when the user click submit.

I am using input.ng-dirty.ng-invalid to style the controls with error. So what I want is to set ng-dirty on required controls (or all controls.. it will be the same for me) when the user submits the form.

Validation is working. I understand why, what I am trying could be wrong, but I found no other way to do the same effect, except something that I think is too complicated to be right.

What I tried was:

<div ng-app>
    <form novalidate>
        <input name="formvalue" type="text" ng-model="formvalue" required />
        <input type="submit" />
    </form>
</div>

http://jsfiddle.net/yq4NG/

like image 666
Fernando Avatar asked Aug 19 '13 23:08

Fernando


People also ask

What is dirty in form validation?

To prevent the validator from displaying errors before the user has a chance to edit the form, you should check for either the dirty or touched states in a control. When the user changes the value in the watched field, the control is marked as "dirty"

What is Ng-dirty in AngularJS?

ng-dirty: The ng-dirty class tells that the form has been made dirty (modified ) by the user. It returns true if the user has modified the form. Return type: Return Boolean True if the form/input field is modified by the user else it returns False.

What is dirty and pristine in a form element?

They are all properties of the input field, and are either true or false . Forms have the following states: $pristine No fields have been modified yet. $dirty One or more have been modified. $invalid The form content is not valid.

What is setPristine ()?

$setPristine();Sets the form to its pristine state. This method sets the form's $pristine state to true, the $dirty state to false, removes the ng-dirty class and adds the ng-pristine class. Additionally, it sets the $submitted state to false. This method will also propagate to all the controls contained in this form.


1 Answers

Let's start by adding angular to your jsfiddle by wrapping it in

<div ng-app>...</div>

http://jsfiddle.net/yq4NG/1/

By default the required field will be validated on input (dirty). If you want to have them validated on submit before any input (pristine), then you can run a function on your submit button that will check for pristine fields and dirty them.

That is what i have done in the example: http://jsfiddle.net/yq4NG/6/

You could probably build a reusable solution using custom formatters and validators but this is a simple on off solution.

EDIT:

Simpler again using just classes: http://jsfiddle.net/yq4NG/8/

EDIT [as suggested by @XMLilley in the comments]:

Because angular doesn't provide a $setDirty() method that's equivalent to $setPristine() we're triggering the $dirty state by simply updating the $viewValue with the contents of the $modelValue. It changes nothing, but simulates a user having manually entered each $pristine field and messed around with the value without changing anything.

like image 195
Matthew.Lothian Avatar answered Oct 19 '22 02:10

Matthew.Lothian