diff --git a/API.md b/API.md index 0e60eea..051db92 100644 --- a/API.md +++ b/API.md @@ -37,6 +37,29 @@ API ``` +### **Use Custom Default Valid/Invalid Message**
+You can customize the default valid/invalid message that will be automatically placed next to your input element. + +```html + + + + +``` + ### **Use a custom Valid Message**
You can also add a custom validation message by using `message-id` attribute. It allows you to place a valid/invalid message wherever you want, a target element must specify an `id` attribute that matches with a value of the `message-id`. diff --git a/dist/angular-validation.js b/dist/angular-validation.js index 2fcb04d..3d6e780 100644 --- a/dist/angular-validation.js +++ b/dist/angular-validation.js @@ -398,7 +398,7 @@ angular.module('validation.directive', ['validation.provider']); * @param ctrl * @returns {} */ - var validFunc = function(element, validMessage, validation, scope, ctrl, attrs) { + var validFunc = function(element, validMessage, validation, scope, ctrl, attrs, param) { var messageToShow = validMessage || $validationProvider.getDefaultMsg(validation).success; var validCallback = $parse(attrs.validCallback); var messageId = attrs.messageId; @@ -411,6 +411,8 @@ angular.module('validation.directive', ['validation.provider']); if (element.attr('no-validation-message')) { messageElem.css('display', 'none'); } else if ($validationProvider.showSuccessMessage && messageToShow) { + messageToShow = angular.isFunction(messageToShow) ? messageToShow(element, attrs, param) : messageToShow; + messageElem.html('').append($compile($validationProvider.getSuccessHTML(messageToShow, element, attrs))(scope)); messageElem.css('display', ''); } else { @@ -436,7 +438,7 @@ angular.module('validation.directive', ['validation.provider']); * @param ctrl * @returns {} */ - var invalidFunc = function(element, validMessage, validation, scope, ctrl, attrs) { + var invalidFunc = function(element, validMessage, validation, scope, ctrl, attrs, param) { var messageToShow = validMessage || $validationProvider.getDefaultMsg(validation).error; var invalidCallback = $parse(attrs.invalidCallback); var messageId = attrs.messageId; @@ -449,6 +451,8 @@ angular.module('validation.directive', ['validation.provider']); if (element.attr('no-validation-message')) { messageElem.css('display', 'none'); } else if ($validationProvider.showErrorMessage && messageToShow) { + messageToShow = angular.isFunction(messageToShow) ? messageToShow(element, attrs, param) : messageToShow; + messageElem.html('').append($compile($validationProvider.getErrorHTML(messageToShow, element, attrs))(scope)); messageElem.css('display', ''); } else { @@ -555,7 +559,7 @@ angular.module('validation.directive', ['validation.provider']); var validationGroup = attrs.validationGroup; var valid = { success: function(message) { - validFunc(element, message || attrs[successMessage], validator, scope, ctrl, attrs); + validFunc(element, message || attrs[successMessage], validator, scope, ctrl, attrs, validatorParam); if (leftValidation.length) { return checkValidation(scope, element, attrs, ctrl, leftValidation, value); } else { @@ -563,7 +567,7 @@ angular.module('validation.directive', ['validation.provider']); } }, error: function(message) { - return invalidFunc(element, message || attrs[errorMessage], validator, scope, ctrl, attrs); + return invalidFunc(element, message || attrs[errorMessage], validator, scope, ctrl, attrs, validatorParam); } }; diff --git a/dist/angular-validation.min.js b/dist/angular-validation.min.js index 2eb67a4..409c208 100644 --- a/dist/angular-validation.min.js +++ b/dist/angular-validation.min.js @@ -1 +1 @@ -angular.module("validation",["validation.provider","validation.directive"]),angular.module("validation.provider",[]),angular.module("validation.directive",["validation.provider"]),function(){function a(){var a,b,c,d,e,f=this,g=function(f){a=f,b=a.get("$rootScope"),c=a.get("$http"),d=a.get("$q"),e=a.get("$timeout")},h={},i=null,j={};this.setExpression=function(a){return angular.extend(h,a),f},this.getExpression=function(a){return h[a]},this.setDefaultMsg=function(a){return angular.extend(j,a),f},this.getDefaultMsg=function(a){return j[a]},this.setValidMethod=function(a){i=a},this.getValidMethod=function(){return i},this.setErrorHTML=function(a){if(a.constructor===Function)return f.getErrorHTML=a,f},this.getErrorHTML=function(a){return'

'+a+"

"},this.setSuccessHTML=function(a){if(a.constructor===Function)return f.getSuccessHTML=a,f},this.getSuccessHTML=function(a){return'

'+a+"

"},this.showSuccessMessage=!0,this.showErrorMessage=!0,this.checkValid=function(a){return!(!a||!a.$valid)},this.validate=function(a){var c=d.defer(),g=0;if(void 0===a)return console.error("This is not a regular Form name scope"),c.reject("This is not a regular Form name scope"),c.promise;if(a.validationId)b.$broadcast(a.$name+"submit-"+a.validationId,g++);else if(a.constructor===Array)for(var h in a)b.$broadcast(a[h].$name+"submit-"+a[h].validationId,g++);else for(var i in a)"$"!==i[0]&&a[i].hasOwnProperty("$dirty")&&b.$broadcast(i+"submit-"+a[i].validationId,g++);return c.promise.success=function(a){return c.promise.then(function(b){a(b)}),c.promise},c.promise.error=function(a){return c.promise.then(null,function(b){a(b)}),c.promise},e(function(){f.checkValid(a)?c.resolve("success"):c.reject("error")}),c.promise},this.validCallback=null,this.invalidCallback=null,this.resetCallback=null,this.reset=function(a){if(void 0===a)return void console.error("This is not a regular Form name scope");if(a.validationId)b.$broadcast(a.$name+"reset-"+a.validationId);else if(a.constructor===Array)for(var c in a)b.$broadcast(a[c].$name+"reset-"+a[c].validationId);else for(var d in a)"$"!==d[0]&&a[d].hasOwnProperty("$dirty")&&b.$broadcast(d+"reset-"+a[d].validationId)},this.addMsgElement=function(a){return a.after("")},this.getMsgElement=function(a){return a.next()},this.$get=["$injector",function(a){return g(a),{setValidMethod:this.setValidMethod,getValidMethod:this.getValidMethod,setErrorHTML:this.setErrorHTML,getErrorHTML:this.getErrorHTML,setSuccessHTML:this.setSuccessHTML,getSuccessHTML:this.getSuccessHTML,setExpression:this.setExpression,getExpression:this.getExpression,setDefaultMsg:this.setDefaultMsg,getDefaultMsg:this.getDefaultMsg,showSuccessMessage:this.showSuccessMessage,showErrorMessage:this.showErrorMessage,checkValid:this.checkValid,validate:this.validate,validCallback:this.validCallback,invalidCallback:this.invalidCallback,resetCallback:this.resetCallback,reset:this.reset,addMsgElement:this.addMsgElement,getMsgElement:this.getMsgElement}}]}angular.module("validation.provider").provider("$validation",a)}.call(this),function(){function a(a){var b=a.get("$validation"),c=a.get("$timeout"),d=a.get("$parse");return{link:function(a,e,f){var g=d(f.validationReset)(a);c(function(){e.on("click",function(a){a.preventDefault(),b.reset(g)})})}}}angular.module("validation.directive").directive("validationReset",a),a.$inject=["$injector"]}.call(this),function(){function a(a){var b=a.get("$validation"),c=a.get("$timeout"),d=a.get("$parse");return{priority:1,require:"?ngClick",link:function(a,e,f){var g=d(f.validationSubmit)(a);c(function(){e.off("click"),e.on("click",function(c){c.preventDefault(),b.validate(g).success(function(){d(f.ngClick)(a)})})})}}}angular.module("validation.directive").directive("validationSubmit",a),a.$inject=["$injector"]}.call(this),function(){function a(a){function b(a,b,c){for(var d=document.querySelectorAll("*[validation-group="+b+"]"),e=0,f=d.length;e0?(b=a[0],angular.isObject(b)||(b={result:b,message:""})):b={result:!1,message:""},b}var d=a.get("$validation"),e=a.get("$q"),f=a.get("$timeout"),g=a.get("$compile"),h=a.get("$parse"),i={},j=function(a,b,c,e,f,i){var j,k=b||d.getDefaultMsg(c).success,l=h(i.validCallback),m=i.messageId,n=i.validationGroup;return j=m||n?angular.element(document.querySelector("#"+(m||n))):d.getMsgElement(a),a.attr("no-validation-message")?j.css("display","none"):d.showSuccessMessage&&k?(j.html("").append(g(d.getSuccessHTML(k,a,i))(e)),j.css("display","")):j.css("display","none"),f.$setValidity(f.$name,!0),l(e,{message:k}),d.validCallback&&d.validCallback(a),!0},k=function(a,b,c,e,f,i){var j,k=b||d.getDefaultMsg(c).error,l=h(i.invalidCallback),m=i.messageId,n=i.validationGroup;return j=m||n?angular.element(document.querySelector("#"+(m||n))):d.getMsgElement(a),a.attr("no-validation-message")?j.css("display","none"):d.showErrorMessage&&k?(j.html("").append(g(d.getErrorHTML(k,a,i))(e)),j.css("display","")):j.css("display","none"),f.$setValidity(f.$name,!1),l(e,{message:k}),d.invalidCallback&&d.invalidCallback(a),!1},l=function(a){var b=i[a];return Object.keys(b).some(function(a){return b[a]})},m={},n=function(a,f,g,h,m,o){var p=m.slice(0),q=p[0].trim(),r=q.indexOf("="),s=r===-1?q:q.substr(0,r),t=r===-1?null:q.substr(r+1),u=p.slice(1),v=s+"SuccessMessage",w=s+"ErrorMessage",x=d.getExpression(s),y=g.validationGroup,z={success:function(b){return j(f,b||g[v],s,a,h,g),!u.length||n(a,f,g,h,u,o)},error:function(b){return k(f,b||g[w],s,a,h,g)}};if(void 0===x)return console.error('You are using undefined validator "%s"',s),u.length?n(a,f,g,h,u,o):void 0;if(x.constructor===Function)return e.all([d.getExpression(s)(o,a,f,g,t)]).then(function(d){var e=c(d),f=e.message;return e.result?(y&&(i[y][h.$name]=!0,b(a,y,!0)),z.success(f)):y?(i[y][h.$name]=!1,l(y)?void b(a,y,!0):(b(a,y,!1),z.error(f))):z.error(f)},function(){return z.error()});if(x.constructor!==RegExp)return z.error();if(void 0!==o&&null!==o){if(d.getExpression(s).test(o))return y&&(i[y][h.$name]=!0,b(a,y,!0)),z.success();if(!y)return z.error();if(i[y][h.$name]=!1,!l(y))return b(a,y,!1),z.error();b(a,y,!0)}},o=function(){return(65536*(1+Math.random())|0).toString(16).substring(1)},p=function(){return o()+o()+o()+o()};return{restrict:"A",require:"ngModel",link:function(a,b,c,e){var g="false"!==c.useViewValue,h=c.validator,j=c.messageId,k=c.validationGroup,l=c.validMethod,o=c.ngModel,q=function(){},r=h.split(","),s=e.validationId=p(),t=null,u=void 0;return"boolean"==typeof c.initialValidity&&(u=c.initialValidity),c.$observe("validator",function(a){r=a.split(",")}),k&&(i[k]||(i[k]={}),i[k][e.$name]=!1),j||k||d.addMsgElement(b),e.$setValidity(e.$name,u),a.$on(e.$name+"reset-"+s,function(){q(),f(function(){e.$setViewValue(t),e.$setPristine(),e.$setValidity(e.$name,void 0),e.$render(),j||k?angular.element(document.querySelector("#"+(j||k))).html(""):d.getMsgElement(b).html(""),d.resetCallback&&d.resetCallback(b)})}),l=angular.isUndefined(l)?d.getValidMethod():l,a.$on(e.$name+"submit-"+s,function(d,h){var i=g?e.$viewValue:e.$modelValue,j=!1;j=n(a,b,c,e,r,i),"submit"===l&&(q(),q=a.$watch(function(){return a.$eval(o)},function(d,f){d!==f&&(void 0!==d&&null!==d||(d=""),j=n(a,b,c,e,r,d))}));var k=function(a){a?delete m[h]:(m[h]=b[0],f(function(){m[Math.min.apply(null,Object.keys(m))].focus()},0))};j.constructor===Object?j.then(k):k(j)}),"blur"===l?void b.bind("blur",function(){var d=a.$eval(o);"$apply"!==a.$root.$$phase?a.$apply(function(){n(a,b,c,e,r,d)}):n(a,b,c,e,r,d)}):void("submit"!==l&&"submit-only"!==l&&(a.$watch(function(){return a.$eval(o)},function(f){if(e.$pristine&&e.$viewValue)t=e.$viewValue||"",e.$setViewValue(e.$viewValue);else if(e.$pristine)return void(j||k?angular.element(document.querySelector("#"+(j||k))).html(""):d.getMsgElement(b).html(""));n(a,b,c,e,r,f)}),f(function(){c.$observe("noValidationMessage",function(a){var c;c=j||k?angular.element(document.querySelector("#"+(j||k))):d.getMsgElement(b),"true"===a||a===!0?c.css("display","none"):"false"!==a&&a!==!1||c.css("display","block")})})))}}}angular.module("validation.directive").directive("validator",a),a.$inject=["$injector"]}.call(this); \ No newline at end of file +angular.module("validation",["validation.provider","validation.directive"]),angular.module("validation.provider",[]),angular.module("validation.directive",["validation.provider"]),function(){function a(){var a,b,c,d,e,f=this,g=function(f){a=f,b=a.get("$rootScope"),c=a.get("$http"),d=a.get("$q"),e=a.get("$timeout")},h={},i=null,j={};this.setExpression=function(a){return angular.extend(h,a),f},this.getExpression=function(a){return h[a]},this.setDefaultMsg=function(a){return angular.extend(j,a),f},this.getDefaultMsg=function(a){return j[a]},this.setValidMethod=function(a){i=a},this.getValidMethod=function(){return i},this.setErrorHTML=function(a){if(a.constructor===Function)return f.getErrorHTML=a,f},this.getErrorHTML=function(a){return'

'+a+"

"},this.setSuccessHTML=function(a){if(a.constructor===Function)return f.getSuccessHTML=a,f},this.getSuccessHTML=function(a){return'

'+a+"

"},this.showSuccessMessage=!0,this.showErrorMessage=!0,this.checkValid=function(a){return!(!a||!a.$valid)},this.validate=function(a){var c=d.defer(),g=0;if(void 0===a)return console.error("This is not a regular Form name scope"),c.reject("This is not a regular Form name scope"),c.promise;if(a.validationId)b.$broadcast(a.$name+"submit-"+a.validationId,g++);else if(a.constructor===Array)for(var h in a)b.$broadcast(a[h].$name+"submit-"+a[h].validationId,g++);else for(var i in a)"$"!==i[0]&&a[i].hasOwnProperty("$dirty")&&b.$broadcast(i+"submit-"+a[i].validationId,g++);return c.promise.success=function(a){return c.promise.then(function(b){a(b)}),c.promise},c.promise.error=function(a){return c.promise.then(null,function(b){a(b)}),c.promise},e(function(){f.checkValid(a)?c.resolve("success"):c.reject("error")}),c.promise},this.validCallback=null,this.invalidCallback=null,this.resetCallback=null,this.reset=function(a){if(void 0===a)return void console.error("This is not a regular Form name scope");if(a.validationId)b.$broadcast(a.$name+"reset-"+a.validationId);else if(a.constructor===Array)for(var c in a)b.$broadcast(a[c].$name+"reset-"+a[c].validationId);else for(var d in a)"$"!==d[0]&&a[d].hasOwnProperty("$dirty")&&b.$broadcast(d+"reset-"+a[d].validationId)},this.addMsgElement=function(a){return a.after("")},this.getMsgElement=function(a){return a.next()},this.$get=["$injector",function(a){return g(a),{setValidMethod:this.setValidMethod,getValidMethod:this.getValidMethod,setErrorHTML:this.setErrorHTML,getErrorHTML:this.getErrorHTML,setSuccessHTML:this.setSuccessHTML,getSuccessHTML:this.getSuccessHTML,setExpression:this.setExpression,getExpression:this.getExpression,setDefaultMsg:this.setDefaultMsg,getDefaultMsg:this.getDefaultMsg,showSuccessMessage:this.showSuccessMessage,showErrorMessage:this.showErrorMessage,checkValid:this.checkValid,validate:this.validate,validCallback:this.validCallback,invalidCallback:this.invalidCallback,resetCallback:this.resetCallback,reset:this.reset,addMsgElement:this.addMsgElement,getMsgElement:this.getMsgElement}}]}angular.module("validation.provider").provider("$validation",a)}.call(this),function(){function a(a){var b=a.get("$validation"),c=a.get("$timeout"),d=a.get("$parse");return{link:function(a,e,f){var g=d(f.validationReset)(a);c(function(){e.on("click",function(a){a.preventDefault(),b.reset(g)})})}}}angular.module("validation.directive").directive("validationReset",a),a.$inject=["$injector"]}.call(this),function(){function a(a){var b=a.get("$validation"),c=a.get("$timeout"),d=a.get("$parse");return{priority:1,require:"?ngClick",link:function(a,e,f){var g=d(f.validationSubmit)(a);c(function(){e.off("click"),e.on("click",function(c){c.preventDefault(),b.validate(g).success(function(){d(f.ngClick)(a)})})})}}}angular.module("validation.directive").directive("validationSubmit",a),a.$inject=["$injector"]}.call(this),function(){function a(a){function b(a,b,c){for(var d=document.querySelectorAll("*[validation-group="+b+"]"),e=0,f=d.length;e0?(b=a[0],angular.isObject(b)||(b={result:b,message:""})):b={result:!1,message:""},b}var d=a.get("$validation"),e=a.get("$q"),f=a.get("$timeout"),g=a.get("$compile"),h=a.get("$parse"),i={},j=function(a,b,c,e,f,i,j){var k,l=b||d.getDefaultMsg(c).success,m=h(i.validCallback),n=i.messageId,o=i.validationGroup;return k=n||o?angular.element(document.querySelector("#"+(n||o))):d.getMsgElement(a),a.attr("no-validation-message")?k.css("display","none"):d.showSuccessMessage&&l?(l=angular.isFunction(l)?l(a,i,j):l,k.html("").append(g(d.getSuccessHTML(l,a,i))(e)),k.css("display","")):k.css("display","none"),f.$setValidity(f.$name,!0),m(e,{message:l}),d.validCallback&&d.validCallback(a),!0},k=function(a,b,c,e,f,i,j){var k,l=b||d.getDefaultMsg(c).error,m=h(i.invalidCallback),n=i.messageId,o=i.validationGroup;return k=n||o?angular.element(document.querySelector("#"+(n||o))):d.getMsgElement(a),a.attr("no-validation-message")?k.css("display","none"):d.showErrorMessage&&l?(l=angular.isFunction(l)?l(a,i,j):l,k.html("").append(g(d.getErrorHTML(l,a,i))(e)),k.css("display","")):k.css("display","none"),f.$setValidity(f.$name,!1),m(e,{message:l}),d.invalidCallback&&d.invalidCallback(a),!1},l=function(a){var b=i[a];return Object.keys(b).some(function(a){return b[a]})},m={},n=function(a,f,g,h,m,o){var p=m.slice(0),q=p[0].trim(),r=q.indexOf("="),s=r===-1?q:q.substr(0,r),t=r===-1?null:q.substr(r+1),u=p.slice(1),v=s+"SuccessMessage",w=s+"ErrorMessage",x=d.getExpression(s),y=g.validationGroup,z={success:function(b){return j(f,b||g[v],s,a,h,g,t),!u.length||n(a,f,g,h,u,o)},error:function(b){return k(f,b||g[w],s,a,h,g,t)}};if(void 0===x)return console.error('You are using undefined validator "%s"',s),u.length?n(a,f,g,h,u,o):void 0;if(x.constructor===Function)return e.all([d.getExpression(s)(o,a,f,g,t)]).then(function(d){var e=c(d),f=e.message;return e.result?(y&&(i[y][h.$name]=!0,b(a,y,!0)),z.success(f)):y?(i[y][h.$name]=!1,l(y)?void b(a,y,!0):(b(a,y,!1),z.error(f))):z.error(f)},function(){return z.error()});if(x.constructor!==RegExp)return z.error();if(void 0!==o&&null!==o){if(d.getExpression(s).test(o))return y&&(i[y][h.$name]=!0,b(a,y,!0)),z.success();if(!y)return z.error();if(i[y][h.$name]=!1,!l(y))return b(a,y,!1),z.error();b(a,y,!0)}},o=function(){return(65536*(1+Math.random())|0).toString(16).substring(1)},p=function(){return o()+o()+o()+o()};return{restrict:"A",require:"ngModel",link:function(a,b,c,e){var g="false"!==c.useViewValue,h=c.validator,j=c.messageId,k=c.validationGroup,l=c.validMethod,o=c.ngModel,q=function(){},r=h.split(","),s=e.validationId=p(),t=null,u=void 0;return"boolean"==typeof c.initialValidity&&(u=c.initialValidity),c.$observe("validator",function(a){r=a.split(",")}),k&&(i[k]||(i[k]={}),i[k][e.$name]=!1),j||k||d.addMsgElement(b),e.$setValidity(e.$name,u),a.$on(e.$name+"reset-"+s,function(){q(),f(function(){e.$setViewValue(t),e.$setPristine(),e.$setValidity(e.$name,void 0),e.$render(),j||k?angular.element(document.querySelector("#"+(j||k))).html(""):d.getMsgElement(b).html(""),d.resetCallback&&d.resetCallback(b)})}),l=angular.isUndefined(l)?d.getValidMethod():l,a.$on(e.$name+"submit-"+s,function(d,h){var i=g?e.$viewValue:e.$modelValue,j=!1;j=n(a,b,c,e,r,i),"submit"===l&&(q(),q=a.$watch(function(){return a.$eval(o)},function(d,f){d!==f&&(void 0!==d&&null!==d||(d=""),j=n(a,b,c,e,r,d))}));var k=function(a){a?delete m[h]:(m[h]=b[0],f(function(){m[Math.min.apply(null,Object.keys(m))].focus()},0))};j.constructor===Object?j.then(k):k(j)}),"blur"===l?void b.bind("blur",function(){var d=a.$eval(o);"$apply"!==a.$root.$$phase?a.$apply(function(){n(a,b,c,e,r,d)}):n(a,b,c,e,r,d)}):void("submit"!==l&&"submit-only"!==l&&(a.$watch(function(){return a.$eval(o)},function(f){if(e.$pristine&&e.$viewValue)t=e.$viewValue||"",e.$setViewValue(e.$viewValue);else if(e.$pristine)return void(j||k?angular.element(document.querySelector("#"+(j||k))).html(""):d.getMsgElement(b).html(""));n(a,b,c,e,r,f)}),f(function(){c.$observe("noValidationMessage",function(a){var c;c=j||k?angular.element(document.querySelector("#"+(j||k))):d.getMsgElement(b),"true"===a||a===!0?c.css("display","none"):"false"!==a&&a!==!1||c.css("display","block")})})))}}}angular.module("validation.directive").directive("validator",a),a.$inject=["$injector"]}.call(this); \ No newline at end of file diff --git a/src/validator.directive.js b/src/validator.directive.js index 707665d..9569d60 100644 --- a/src/validator.directive.js +++ b/src/validator.directive.js @@ -20,7 +20,7 @@ * @param ctrl * @returns {} */ - var validFunc = function(element, validMessage, validation, scope, ctrl, attrs) { + var validFunc = function(element, validMessage, validation, scope, ctrl, attrs, param) { var messageToShow = validMessage || $validationProvider.getDefaultMsg(validation).success; var validCallback = $parse(attrs.validCallback); var messageId = attrs.messageId; @@ -33,6 +33,8 @@ if (element.attr('no-validation-message')) { messageElem.css('display', 'none'); } else if ($validationProvider.showSuccessMessage && messageToShow) { + messageToShow = angular.isFunction(messageToShow) ? messageToShow(element, attrs, param) : messageToShow; + messageElem.html('').append($compile($validationProvider.getSuccessHTML(messageToShow, element, attrs))(scope)); messageElem.css('display', ''); } else { @@ -58,7 +60,7 @@ * @param ctrl * @returns {} */ - var invalidFunc = function(element, validMessage, validation, scope, ctrl, attrs) { + var invalidFunc = function(element, validMessage, validation, scope, ctrl, attrs, param) { var messageToShow = validMessage || $validationProvider.getDefaultMsg(validation).error; var invalidCallback = $parse(attrs.invalidCallback); var messageId = attrs.messageId; @@ -71,6 +73,8 @@ if (element.attr('no-validation-message')) { messageElem.css('display', 'none'); } else if ($validationProvider.showErrorMessage && messageToShow) { + messageToShow = angular.isFunction(messageToShow) ? messageToShow(element, attrs, param) : messageToShow; + messageElem.html('').append($compile($validationProvider.getErrorHTML(messageToShow, element, attrs))(scope)); messageElem.css('display', ''); } else { @@ -177,7 +181,7 @@ var validationGroup = attrs.validationGroup; var valid = { success: function(message) { - validFunc(element, message || attrs[successMessage], validator, scope, ctrl, attrs); + validFunc(element, message || attrs[successMessage], validator, scope, ctrl, attrs, validatorParam); if (leftValidation.length) { return checkValidation(scope, element, attrs, ctrl, leftValidation, value); } else { @@ -185,7 +189,7 @@ } }, error: function(message) { - return invalidFunc(element, message || attrs[errorMessage], validator, scope, ctrl, attrs); + return invalidFunc(element, message || attrs[errorMessage], validator, scope, ctrl, attrs, validatorParam); } }; diff --git a/test/unit/providerSpec.js b/test/unit/providerSpec.js index dd977f7..0e3b08a 100644 --- a/test/unit/providerSpec.js +++ b/test/unit/providerSpec.js @@ -65,6 +65,71 @@ describe('provider', function() { } })); + it('set/get DefaultMsg (function)', inject(function() { + var defaultMessages = { + field: { + error: function(element, attrs, param) { + return 'error'; + }, + success: function(element, attrs, param) { + return 'success'; + } + } + }; + + validationProvider.setDefaultMsg(defaultMessages); + + expect(validationProvider.getDefaultMsg('field')).toEqual(defaultMessages.field); + for (var key in validationProvider.getDefaultMsg('field')) { + expect(key).toMatch(/^error$|^success$/); + expect(validationProvider.getDefaultMsg('field')[key]).toEqual(defaultMessages.field[key]); + } + })); + + it('defaultMessage (function) error displays expected message', inject(function() { + var messageSpy = jasmine.createSpy('messageSpy'); + + validationProvider.setDefaultMsg({ + minlength: { + error: function(element, attrs, param) { + messageSpy(); + return element.attr('name') + ' ' + attrs.class + ' ' + param; + } + } + }); + + var messageElement = $compile('
')($scope); + $scope.Form.messagefield.$setViewValue('xxx'); + validationProvider.validate($scope.Form); + + var messageSpan = messageElement.find('span'); + expect(messageSpan.css('display')).toBe(''); + expect(messageSpan.find('p').text()).toBe('messagefield messagefieldclass 5'); + expect(messageSpy).toHaveBeenCalled(); + })); + + it('defaultMessage (function) success displays expected message', inject(function() { + var messageSpy = jasmine.createSpy('messageSpy'); + + validationProvider.setDefaultMsg({ + minlength: { + success: function(element, attrs, param) { + messageSpy(); + return element.attr('name') + ' ' + attrs.class + ' ' + param; + } + } + }); + + var messageElement = $compile('
')($scope); + $scope.Form.messagefield.$setViewValue('xxxxxx'); + validationProvider.validate($scope.Form); + + var messageSpan = messageElement.find('span'); + expect(messageSpan.css('display')).toBe(''); + expect(messageSpan.find('p').text()).toBe('messagefield messagefieldclass 5'); + expect(messageSpy).toHaveBeenCalled(); + })); + it('set/get successHTML', inject(function() { validationProvider.setSuccessHTML('sethtml'); expect(validationProvider.getSuccessHTML('true')).not.toEqual('sethtml');