- Forums
- AngularJS
- how to show the state of the form in angularJs
on this topic i will show you how you can display or show all the states in a form for validation. [4577], Last Updated: Mon Jun 24, 2024
support
Thu Dec 10, 2015
0 Comments
521 Visits
i was creating a web form and i wanted to add some validation to the form, angularJS offer validation in the form of $valid for example, but if i have a field and i want to see what is that state for example i want to know if its dirty or not? how can you do that. well, its very easy, all you have to do is use the name of the form as the directive and it will show you the state of the form. this is the example html:
exampleForm
{{exampleForm | json}}
<form name="exampleForm">
email: <br>
<input type="email" ng-model="exampleForm.email" required formnovalidate><br>
<input type="button" value="Submit">
</form>
exampleForm<pre>{{exampleForm | json}} </pre>
try it, as you type, it will show you what state the form is, in my example above, the output looks like this:
{
"$error": {
"required": [
{
"$viewValue": "",
"$validators": {},
"$asyncValidators": {},
"$parsers": [],
"$formatters": [
null
],
"$viewChangeListeners": [],
"$untouched": false,
"$touched": true,
"$pristine": false,
"$dirty": true,
"$valid": false,
"$invalid": true,
"$error": {
"required": true
},
"$name": "",
"$options": null
}
]
},
"$name": "exampleForm",
"$dirty": true,
"$pristine": false,
"$valid": false,
"$invalid": true,
"$submitted": false
}