This tutorial is out of date and no longer maintained.
Angular has always strived to provide tools to make forms easier. Just using Angular to submit AJAX forms and using Angular’s built-in validation makes using forms that much easier.
There are even great 3rd party modules to help us work with forms like angular-formly
.
Since Angular 1.3, there is a new tool for creating and managing forms in AngularJS, ngMessages. This module specifically helps us deal with displaying error messages from form validation.
Let’s take a quick look at how forms look without using this module.
We are explicitly showing each error message only if that error exists. This can get tedious when we have multiple errors that we want to show.
This is where ngMessages comes in. This module brings some sanity to validation messages.
Let’s take the above example and see how that would look in ngMessages.
Much simpler! ngMessages will handle showing and hiding specific messages based on the errors. ngMessages is basically looping through the userForm.name.$errors
object and displaying messages based on that.
The setup for ngMessages is very simple. We just need to load the module after Angular and then inject it into our application.
Now we can inject it into our application in the app.js
file.
Just use the ng-messages
directive and pass in the field you want and its $error
object.
This is the format:
Let’s create a simple sample application to demonstrate ngMessages in practice. We’re working from a Plunkr. You can create a new one for yourself or just follow along in the code provided.
We’re going to be using some very simple HTML here. We just need a form after all.
Here’s our index.html
file:
This will be the starting template for our app. We are using novalidate
on our form so that we disable the HTML5 validations. We have our own validations and they look much better.
We’ve started our HTML. Now we just need to create the Angular application that we already referenced in app.js
, ng-app
, and ng-controller
.
Create an app.js
file and use the following:
We don’t need to have anything in our controller right now, but this is where you would process your form.
name
The name
field only has three validations (minlength
, maxlength
, required
).
Here are the ng-messages for this input:
email
For our email
input, let’s take a different approach. If our form has multiple fields, then it can be tedious to create multiple ng-messages
blocks. ngMessages gives us the ability to pull messages from an external file.
This means we can reuse the same messages for multiple fields!
Let’s create a new file called messages.html
. We can place all of our messages in this file and just call it with ng-messages-include
.
Here’s the messages.html
file:
Now we can use it for our email
input:
This makes ngMessages very powerful and reusable.
Let’s say we wanted to only show error messages after a user has clicked out of the input that they are typing into. It isn’t very intuitive to show errors even before a user has used an input
.
Angular provides a simple way to do this. We just have to use ngShow and the $touched
validation feature Angular provides.
For example, we can only show errors for the name
input using the following:
Now, these messages will only show after an input blur.
We also want to use the Bootstrap provided classes (.has-error
) to highlight the field as red if there is an error. We can use ngClass to add the error class if the field is $invalid
.
We are adding the .has-error
class if this field has been $touch
ed and $invalid
.
With this simple module, Angular form validation has gotten that much easier. Try it out in your own applications and let us know how you like it. Do you prefer ngMessages, a third-party software like angular-formly
, or doing validation from scratch?
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!