AngularJS and web forms are a match made in heaven. AngularJS version 1.3 has made it easier than ever for developers to build powerful, user-friendly, and maintainable web forms. Users have certain expectations when confronted with web forms and developers should anticipate these expectations. Therefore, removing any and all obstacles between the user and a completed, valid form is of upmost importance!
Isn’t it annoying when you fill out a form, hit submit, wait, then the page jumps to the top with some cryptic red text telling you how bad you are at filling out forms? Users hate that. Everyone hates that! Let’s empathize with our users and create a form the sets them up for success.
Enter front-end validation. With the 1.3 release, the Angular JS team bestowed onto us the ng-messages module. This is a tremendous improvement in how we build validation into forms. The module allows developers to have a list of generic error messages for common validation errors as well as craft custom messages for those more complex field types.
Here’s how I use the
ng-messages module on a form field:
<div class="error-message" ng-messages="loginForm.email.$error" ng-if="loginForm.email.$dirty" ng-messages-include="views/widgets/formErrors.html"> </div>
Let’s break down the different directives used here. First, the
ng-messages directive on the
<div> refers to the error object for the field you are validating. The module will detect any properties on the $error object and display the appropriate error template. Next, is the
ng-messages-include directive. This references a template for all of my generic error messages (required, min-length, max-length, etc.). Using a template for these common errors allows very easy maintenance and consistency throughout the application.
Example of a generic message in the
<div ng-message="required">This field is required.</div>
For custom validators, just replace the “required” value with the name of the validator (ex: “pattern”).
Also, the ng-if directive set to the field’s $dirty flag allows us to hide the validation messages ‘till the user enters something into the field. Thus, preventing the form from displaying a bunch of “errors” upon first load.
Users expect most forms to work the exact same way on the web. If the user has to spend unnecessary time figuring out how to fill out your form, it builds resentment and frustration; damaging the trust between you and your users. Keep the form consistent and make a point to ensure simple tasks are done right! For example, the “submit” button.
Hitting enter should submit the form. This sounds simple enough, but it’s very easy to overlook. Developers often attach the
ng-click directive to a button, point it to a function to submit the form data and call it a day. Without using the
ng-submit directive on the form tag, hitting enter won’t do anything to your form. This is especially important on small, short forms like search or sign up because the user is just expecting to submit using the enter key.
Here’s what that form might look like:
<form ng-submit="vm.submitForm()"> <label for="name">Username:</label> <input type="text" name="username" id="username" ng-model="vm.name" /> <label for="password">Password:</label> <input type="text" name="password" id="password" ng-model="vm.password" /> <button type="submit">Submit</button> </form>
Coding forms can be very repetitive, especially when we include the markup for validation. Creating templates for commonly used fields and markup can definitely help keep your code DRY (Don’t Repeat Yourself). You could even go so far as to creating directives for each type of field or form component. Well, I have good news for you. Someone has already done just that: https://github.com/formly-js/angular-formly. This module allows you to keep templates for each type of field and dynamically generate forms using a JSON object. If you’re interested in easily generating forms throughout your application and keeping all forms consistent, angular-formly may save you a ton of time.
Following these easy patterns will keep your users happy clicking and tapping through your application. Let’s face it, forms aren’t the most exciting feature, but they are necessary. Why not make them quick and easy and move on to those more awesome features you have to offer?
Want to dig deeper? Check out my codepen to see some of the patterns we discussed in action.