I have done it correctly by taking reference fro, official docs of angularjs.
But in directive's link method, when I set the validity using $setValidity() method, it does not reflects in view part using {{formname.controlname.$error.validationErrorKey}}
Please help me to track the error, or mistake that I am doing.
Thanks in advance
<form name="form" novalidate>
URL <input type="text" ng-model="myURL" name="myURL" my-url /> {{form.myURL.$error.myUrlError}}
<span class="errorMessage" ng-show="form.myURL.$dirty && form.myURL.$error.myUrlError">
please enter correct url
</span>
</form>
validationModule.directive("myUrl", function($window) {
//return Directive Definition Object (DDO)
return{
restrict:"A",
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
elm.bind('blur',function() {
if (ctrl.$isEmpty(ctrl.$viewValue)) {
console.log('isEMpty-' + new Date());
ctrl.$setValidity("myUrlError", true);
} else {
var URL_REGEXP= /https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,}/;
if (URL_REGEXP.test(ctrl.$viewValue)) {
console.log("valid-" + new Date());
ctrl.$setValidity("myUrlError", true);
} else {
console.log("invalid-" + new Date());
ctrl.$setValidity("myUrlError", false);
}
}
}); //end if 'blur' event listener
}//end of link function
};//end of DDO
});