AngularJS - Trigger Validation

/** * @example * In Template: <form name="myForm">...</form> * In Controller: validate($scope.myForm); */ // TypeScript Version validate(form: angular.IFormController) { angular.forEach(form, (control: angular.INgModelController, name: string) => { // Excludes internal angular properties if (typeof name === 'string' && name.charAt(0) !== '$') { // To display ngMessages control.$setTouched(); // Runs each of the registered validators control.$validate(); } }); } // JavaScript Version function validate(form) { angular.forEach(form, function(control, name) { // Excludes internal angular properties if (typeof name === 'string' && name.charAt(0) !== '$') { // To display ngMessages control.$setTouched(); // Runs each of the registered validators control.$validate(); } }); }
If you need to run all the validators of a form at once, just call this function in your ngController.
You can also put it in a Service, to centralize the utility.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.