16.07.2017 Views

AngularJS Essentials

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3<br />

<br />

<br />

For the form, avoid using the name that has already been used inside the ngModel<br />

directive; otherwise, we will not be able to perform the validation properly. It would<br />

be nice to use some suffix for both the form and the field names as that would help to<br />

make things clearer, thus avoiding mistakes.<br />

Basic validation<br />

The validation process is quite simple and relies on some directives to do the job. The<br />

first one that we need to understand is the ngRequired directive. It could be attached<br />

to any field of the form in order to intimate the validation process that the field is<br />

actually required:<br />

<br />

In addition to this, we could be a little more specific by using the ngMinlength and<br />

ngMaxlength directives. It is really useful to fulfill some kinds of requirements such<br />

as defining a minimum or maximum limit to each field.<br />

In the following code, we are going to add a basic validation to our parking<br />

application. From now on, the field plate will be a required parameter and<br />

will also have minimum and maximum limits:<br />

<br />

[ 63 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!