validation - How can I extend AngularJS model flags on an Angular form? -
angularjs provides call 'model flags' on forms. example, can have formname.$dirty, formname.$invalid, etc. want know how can create own custom flag angularjs forms? high level demonstration or link article sufficient answer.
see here: how-to-add-custom-validation-to-an-angular-js-form.
in short, custom valitation directive example:
app.directive('blacklist', function (){ return { require: 'ngmodel', link: function(scope, elem, attr, ngmodel) { var blacklist = attr.blacklist.split(','); // 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 it's example of it's usage:
<form name="myform" ng-submit="dosomething()"> <input type="text" name="fruitname" ng-model="data.fruitname" blacklist="coconuts,bananas,pears" required/> <span ng-show="myform.fruitname.$error.blacklist"> phrase "{{data.fruitname}}" blacklisted</span> <span ng-show="myform.fruitname.$error.required">required</span> <button type="submit" ng-disabled="myform.$invalid">submit</button> </form>
but, again, read referenced question , accepted answer, it's far more complete...
Comments
Post a Comment