How to add custom validation to an AngularJS form?

后端 未结 12 1497
既然无缘
既然无缘 2020-11-22 10:07

I have a form with input fields and validation setup by adding the required attributes and such. But for some fields I need to do some extra validation. How wou

12条回答
  •  盖世英雄少女心
    2020-11-22 11:00

    Edit: added information about ngMessages (>= 1.3.X) below.

    Standard form validation messages (1.0.X and above)

    Since this is one of the top results if you Google "Angular Form Validation", currently, I want to add another answer to this for anyone coming in from there.

    There's a method in FormController.$setValidity but that doesn't look like a public API so I rather not use it.

    It's "public", no worries. Use it. That's what it's for. If it weren't meant to be used, the Angular devs would have privatized it in a closure.

    To do custom validation, if you don't want to use Angular-UI as the other answer suggested, you can simply roll your own validation directive.

    app.directive('blacklist', function (){ 
       return {
          require: 'ngModel',
          link: function(scope, elem, attr, ngModel) {
              var blacklist = attr.blacklist.split(',');
    
              //For DOM -> model validation
              ngModel.$parsers.unshift(function(value) {
                 var valid = blacklist.indexOf(value) === -1;
                 ngModel.$setValidity('blacklist', valid);
                 return valid ? value : undefined;
              });
    
              //For model -> DOM validation
              ngModel.$formatters.unshift(function(value) {
                 ngModel.$setValidity('blacklist', blacklist.indexOf(value) === -1);
                 return value;
              });
          }
       };
    });
    

    And here's some example usage:

    The phrase "{{data.fruitName}}" is blacklisted required

    Note: in 1.2.X it's probably preferrable to substitute ng-if for ng-show above

    Here is an obligatory plunker link

    Also, I've written a few blog entries about just this subject that goes into a little more detail:

    Angular Form Validation

    Custom Validation Directives

    Edit: using ngMessages in 1.3.X

    You can now use the ngMessages module instead of ngShow to show your error messages. It will actually work with anything, it doesn't have to be an error message, but here's the basics:

    1. Include
    2. Reference ngMessages in your module declaration:

      var app = angular.module('myApp', ['ngMessages']);
      
    3. Add the appropriate markup:

      required
      invalid email

    In the above markup, ng-message="personForm.email.$error" basically specifies a context for the ng-message child directives. Then ng-message="required" and ng-message="email" specify properties on that context to watch. Most importantly, they also specify an order to check them in. The first one it finds in the list that is "truthy" wins, and it will show that message and none of the others.

    And a plunker for the ngMessages example

提交回复
热议问题