Dmitry K answers this perfectly.
I am going to expand the answer.
//Function before show your form: vm.showForm(form){ form.$setPristine(); form.$setUntouched(); form.myFieldName.$setValidity('myCustomValidationName', false); //More code... } //funtion to validate field on "ng-change" vm.validateField(form){ if(xxxx == yyy) //Make your own validation{ form.myFieldName.$setValidity('myCustomValidationName', true); }else{ form.myFieldName.$setValidity('myCustomValidationName', false); } }
And the corresponding HTML code:
<form name="myFormName" novalidate> <md-input-container class="md-block"> <label>myField</label> <input ng-model="ctrl.myFieldName" name="myFieldName" ng-change="ctrl.validateField(myFormName)" /> <div ng-show="myFormName.myFieldName.$touched || myFormName.$submitted"> <div ng-messages="myFormName.myFieldName.$error"> <div ng-message="myCustomValidationName">this is the message to show</div> </div> </div> </md-input-container> </form>
Pablo sanchez manzano
source share