View on GitHub

lc-validation-summary

Angular directive for providing a validation summary to angular based forms.

Download this project as a .zip file Download this project as a tar.gz file


Distribution streams

You can download the source code folder or install this component through bower.
Download
Download, unzip the source code folder and add the build files to your project.
Install with Bower
Install and use lc-validation-summary using Bower
$ bower install lc-validation-summary

Dependencies

Getting started

  1. Add references to AngularJS and lc-validation-summary's js and css files
  2. Where you declare your app module, add lc-validation-summary:
  3. angular.module('myApp',['lc-validation-summary']);
  4. Wrap your html form with the directive lc-validations-container:
  5. <div lc-validations-container>
      <form name="userForm">
        <label for="userName">Name:</label>
        <input name="userName"
               type="text"
               ng-model="model.name"/>
      </form>
    </div>
  6. Now, each input in the form you want to validate must contain a lc-validation-bubble wich will notify the non-passing validations to the lc-validations-container. Then add one of the default supported validations or add your custom validation (in this example we use ng-required) and define a validation-friendly-name to show in the view
  7. <div lc-validations-container>
      <form name="userForm">
        <label for="userName">Name:</label>
        <input name="userName"
               type="text"
               ng-model="model.name"
               ng-required="true"
               lc-validation-bubble=""
               validation-friendly-name="User Name" />
      </form>
    </div>
    Note: Don't miss adding the name attribute to the <form> and each of your <input> elements.
  8. In your html file, within the controller where you want to use lcValidationSummary, add a div element with thelc-validation-summary directive. This div will show the validation results to the user interface :
  9. <div lc-validation-summary=""></div>

And we're done! Now the validation summary will notify the user when the input is empty.This is the final code and result:

In this simple tutorial, the validation message "this field is mandatory", is the associated message to the angular ng-required directive. Check out more advanced samples to see how to set your custom messages and custom directives .

Default values

Default supported Validations

The default supported validations contained in lcValidationProvider and their default custom error messages are:

Supported validation Default error message.
ng-required "This field is mandatory"
ng-pattern "This field doesn't match the pattern set"
ng-minlenth "This field exceeds the maximum length"
ng-maxlength "This field doesn't reach the minimum length"
Sample code in: Sample 01 - Using different angular validations

Customize

Adding custom Validations

You can add your custom validation directives making use of the validationContainerServiceProvider in your app.config(configFn) function:
  1. Register your custom Validation in your app
  2. myApp.directive('myCustomValidation',fcn(...){...});
  3. Use the method addValidation() from the validationContainerServiceProvider to add a validation item with the following fields:
    { type: 'myCustomDirective', friendlyDescription: 'myErrorMessage' }
  4. In the html code, add your directive to an input field
  5. <input name="myInput"
           my-custom-validation=""
           lc-validation-bubble=""
           validation-friendly-name="myInputFriendlyName"/>
Sample code in: Sample 02 - Using a custom validation

Adding custom error messages

You can also add your custom messages by making use of the validation-custom-error-message parameter:
  1. Add the parameter validation-custom-error-directive and set it to the name of the validation you want the input field to have. Wether it's a custom or a default one.
  2. Add the parameter validation-custom-error-message and set it to the message you want to show when the validation is not passing.
  3. <input name="myInput"
           validation-custom-error-directive="myCustomValidation"
           validation-custom-error-message="My custom error mesage"
           validation-friendly-name="myInputFriendlyName"
           lc-validation-bubble=""/>
               
Sample code in: Sample 03 - Adding custom error messages

Using a custom template

You can also add your custom template by overwritting the lcvalidationSummary-tpl.js template code:
  1. Create a new js file and name it whatever you want.
  2. Copy the content of lcvalidationSummary-tpl.js into your new file.
  3. Overwrite the template with your custom style and html.
Sample code in: Sample 04 - Using a custom template

 

Code Samples

Sample 00 - Validating two input fields

This sample moves one small step forward by adding another input field to the form.

Sample 01 - Using different angular validations

You can use a set of different angular built in validations and apply them to each input in a form.

Sample 02 - Using a custom validation

Here is where the fun stuff starts! Use your own angular directives to validate input fields your own way. In this sample we're using a home-made directive named check-two-fields-match that validates if a couple fields are matching. Usually needed when requiring e-mail addresses.

Sample 03 - Adding custom error messages

Add custom error messages to your directive by using the parameter validation-custom-error-message

Sample 04 - Using a custom template

Overwrite the lcValidationSummary. template by simply linking to a custom template file lcValidationSummary-tpl.js