diff --git a/dist/angular-validation.js b/dist/angular-validation.js index 34f394e..04d5248 100644 --- a/dist/angular-validation.js +++ b/dist/angular-validation.js @@ -399,7 +399,8 @@ angular.module('validation.directive', ['validation.provider']); * @returns {} */ var validFunc = function(element, validMessage, validation, scope, ctrl, attrs) { - var messageToShow = validMessage || $validationProvider.getDefaultMsg(validation).success; + var successMessage = validation + 'SuccessMessage'; + var messageToShow = (attrs[successMessage] !== null && attrs[successMessage] !== undefined) ? (validMessage || $validationProvider.getDefaultMsg(validation).success) : ""; var validCallback = $parse(attrs.validCallback); var messageId = attrs.messageId; var validationGroup = attrs.validationGroup; diff --git a/dist/angular-validation.min.js b/dist/angular-validation.min.js index 4d75ee0..f4e75e8 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,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;return"boolean"==typeof a.initialValidity&&(g=a.initialValidity),k&&(i[k]||(i[k]={}),i[k][e.$name]=!1),j||k||d.addMsgElement(b),e.$setValidity(e.$name,g),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,g){var h=e.$viewValue,i=!1;i=n(a,b,c,e,r,h),"submit"===l&&(q(),q=a.$watch(function(){return a.$eval(o)},function(d,f){d!==f&&(void 0!==d&&null!==d||(d=""),i=n(a,b,c,e,r,d))}));var j=function(a){a?delete m[g]:(m[g]=b[0],f(function(){m[Math.min.apply(null,Object.keys(m))].focus()},0))};i.constructor===Object?i.then(j):j(i)}),"blur"===l?void b.bind("blur",function(){var d=a.$eval(o);a.$apply(function(){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 n(){var i,n,s,r,u,t=this,h=function(t){i=t;n=i.get("$rootScope");s=i.get("$http");r=i.get("$q");u=i.get("$timeout")},f={},e=null,o={};this.setExpression=function(n){return angular.extend(f,n),t};this.getExpression=function(n){return f[n]};this.setDefaultMsg=function(n){return angular.extend(o,n),t};this.getDefaultMsg=function(n){return o[n]};this.setValidMethod=function(n){e=n};this.getValidMethod=function(){return e};this.setErrorHTML=function(n){if(n.constructor===Function)return t.getErrorHTML=n,t};this.getErrorHTML=function(n){return'

'+n+"<\/p>"};this.setSuccessHTML=function(n){if(n.constructor===Function)return t.getSuccessHTML=n,t};this.getSuccessHTML=function(n){return'

'+n+"<\/p>"};this.showSuccessMessage=!0;this.showErrorMessage=!0;this.checkValid=function(n){return!!(n&&n.$valid)};this.validate=function(i){var f=r.defer(),o=0,s,e;if(i===undefined)return console.error("This is not a regular Form name scope"),f.reject("This is not a regular Form name scope"),f.promise;if(i.validationId)n.$broadcast(i.$name+"submit-"+i.validationId,o++);else if(i.constructor===Array)for(s in i)n.$broadcast(i[s].$name+"submit-"+i[s].validationId,o++);else for(e in i)e[0]!=="$"&&i[e].hasOwnProperty("$dirty")&&n.$broadcast(e+"submit-"+i[e].validationId,o++);return f.promise.success=function(n){return f.promise.then(function(t){n(t)}),f.promise},f.promise.error=function(n){return f.promise.then(null,function(t){n(t)}),f.promise},u(function(){t.checkValid(i)?f.resolve("success"):f.reject("error")}),f.promise};this.validCallback=null;this.invalidCallback=null;this.resetCallback=null;this.reset=function(t){var r,i;if(t===undefined){console.error("This is not a regular Form name scope");return}if(t.validationId)n.$broadcast(t.$name+"reset-"+t.validationId);else if(t.constructor===Array)for(r in t)n.$broadcast(t[r].$name+"reset-"+t[r].validationId);else for(i in t)i[0]!=="$"&&t[i].hasOwnProperty("$dirty")&&n.$broadcast(i+"reset-"+t[i].validationId)};this.addMsgElement=function(n){return n.after("<\/span>")};this.getMsgElement=function(n){return n.next()};this.$get=["$injector",function(n){return h(n),{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",n)}.call(this),function(){function n(n){var t=n.get("$validation"),i=n.get("$timeout"),r=n.get("$parse");return{link:function(n,u,f){var e=r(f.validationReset)(n);i(function(){u.on("click",function(n){n.preventDefault();t.reset(e)})})}}}angular.module("validation.directive").directive("validationReset",n);n.$inject=["$injector"]}.call(this),function(){function n(n){var i=n.get("$validation"),r=n.get("$timeout"),t=n.get("$parse");return{priority:1,require:"?ngClick",link:function(n,u,f){var e=t(f.validationSubmit)(n);r(function(){u.off("click");u.on("click",function(r){r.preventDefault();i.validate(e).success(function(){t(f.ngClick)(n)})})})}}}angular.module("validation.directive").directive("validationSubmit",n);n.$inject=["$injector"]}.call(this),function(){function n(n){function r(n,t,i){for(var u=document.querySelectorAll("*[validation-group="+t+"]"),r=0,o=u.length;r0?(t=n[0],angular.isObject(t)||(t={result:t,message:""})):t={result:!1,message:""},t}var t=n.get("$validation"),l=n.get("$q"),o=n.get("$timeout"),s=n.get("$compile"),h=n.get("$parse"),i={},a=function(n,i,r,u,f,e){var l=r+"SuccessMessage",c=e[l]!==null&&e[l]!==undefined?i||t.getDefaultMsg(r).success:"",y=h(e.validCallback),a=e.messageId,v=e.validationGroup,o;return o=a||v?angular.element(document.querySelector("#"+(a||v))):t.getMsgElement(n),n.attr("no-validation-message")?o.css("display","none"):t.showSuccessMessage&&c?(o.html("").append(s(t.getSuccessHTML(c,n,e))(u)),o.css("display","")):o.css("display","none"),f.$setValidity(f.$name,!0),y(u,{message:c}),t.validCallback&&t.validCallback(n),!0},v=function(n,i,r,u,f,e){var c=i||t.getDefaultMsg(r).error,v=h(e.invalidCallback),l=e.messageId,a=e.validationGroup,o;return o=l||a?angular.element(document.querySelector("#"+(l||a))):t.getMsgElement(n),n.attr("no-validation-message")?o.css("display","none"):t.showErrorMessage&&c?(o.html("").append(s(t.getErrorHTML(c,n,e))(u)),o.css("display","")):o.css("display","none"),f.$setValidity(f.$name,!1),v(u,{message:c}),t.invalidCallback&&t.invalidCallback(n),!1},c=function(n){var t=i[n];return Object.keys(t).some(function(n){return t[n]})},f={},u=function(n,f,e,o,s,h){var tt=s.slice(0),k=tt[0].trim(),d=k.indexOf("="),w=d===-1?k:k.substr(0,d),it=d===-1?null:k.substr(d+1),g=tt.slice(1),rt=w+"SuccessMessage",ut=w+"ErrorMessage",nt=t.getExpression(w),p=e.validationGroup,b={success:function(t){return a(f,t||e[rt],w,n,o,e),g.length?u(n,f,e,o,g,h):!0},error:function(t){return v(f,t||e[ut],w,n,o,e)}};if(nt===undefined)return(console.error('You are using undefined validator "%s"',w),g.length)?u(n,f,e,o,g,h):void 0;if(nt.constructor===Function)return l.all([t.getExpression(w)(h,n,f,e,it)]).then(function(t){var f=y(t),u=f.message;if(f.result)return p&&(i[p][o.$name]=!0,r(n,p,!0)),b.success(u);if(p)if(i[p][o.$name]=!1,c(p))r(n,p,!0);else return r(n,p,!1),b.error(u);else return b.error(u)},function(){return b.error()});if(nt.constructor===RegExp){if(h!==undefined&&h!==null){if(t.getExpression(w).test(h))return p&&(i[p][o.$name]=!0,r(n,p,!0)),b.success();if(p)if(i[p][o.$name]=!1,c(p))r(n,p,!0);else return r(n,p,!1),b.error();else return b.error()}}else return b.error()},e=function(){return((1+Math.random())*65536|0).toString(16).substring(1)},p=function(){return e()+e()+e()+e()};return{restrict:"A",require:"ngModel",link:function(n,r,e,s){var d=e.validator,c=e.messageId,h=e.validationGroup,l=e.validMethod,v=e.ngModel,y=function(){},a=d.split(","),w=s.validationId=p(),b=null,k;if(typeof n.initialValidity=="boolean"&&(k=n.initialValidity),h&&(i[h]||(i[h]={}),i[h][s.$name]=!1),c||h||t.addMsgElement(r),s.$setValidity(s.$name,k),n.$on(s.$name+"reset-"+w,function(){y();o(function(){s.$setViewValue(b);s.$setPristine();s.$setValidity(s.$name,undefined);s.$render();c||h?angular.element(document.querySelector("#"+(c||h))).html(""):t.getMsgElement(r).html("");t.resetCallback&&t.resetCallback(r)})}),l=angular.isUndefined(l)?t.getValidMethod():l,n.$on(s.$name+"submit-"+w,function(t,i){var p=s.$viewValue,h=!1,c;h=u(n,r,e,s,a,p);l==="submit"&&(y(),y=n.$watch(function(){return n.$eval(v)},function(t,i){t!==i&&((t===undefined||t===null)&&(t=""),h=u(n,r,e,s,a,t))}));c=function(n){n?delete f[i]:(f[i]=r[0],o(function(){f[Math.min.apply(null,Object.keys(f))].focus()},0))};h.constructor===Object?h.then(c):c(h)}),l==="blur"){r.bind("blur",function(){var t=n.$eval(v);n.$apply(function(){u(n,r,e,s,a,t)})});return}l!=="submit"&&l!=="submit-only"&&(n.$watch(function(){return n.$eval(v)},function(i){if(s.$pristine&&s.$viewValue)b=s.$viewValue||"",s.$setViewValue(s.$viewValue);else if(s.$pristine){c||h?angular.element(document.querySelector("#"+(c||h))).html(""):t.getMsgElement(r).html("");return}u(n,r,e,s,a,i)}),o(function(){e.$observe("noValidationMessage",function(n){var i;i=c||h?angular.element(document.querySelector("#"+(c||h))):t.getMsgElement(r);n==="true"||n===!0?i.css("display","none"):(n==="false"||n===!1)&&i.css("display","block")})}))}}}angular.module("validation.directive").directive("validator",n);n.$inject=["$injector"]}.call(this); \ No newline at end of file diff --git a/src/validator.directive.js b/src/validator.directive.js index 5b39e50..8c8aeac 100644 --- a/src/validator.directive.js +++ b/src/validator.directive.js @@ -21,7 +21,8 @@ * @returns {} */ var validFunc = function(element, validMessage, validation, scope, ctrl, attrs) { - var messageToShow = validMessage || $validationProvider.getDefaultMsg(validation).success; + var successMessage = validation + 'SuccessMessage'; + var messageToShow = (attrs[successMessage] !== null && attrs[successMessage] !== undefined) ? (validMessage || $validationProvider.getDefaultMsg(validation).success) : ""; var validCallback = $parse(attrs.validCallback); var messageId = attrs.messageId; var validationGroup = attrs.validationGroup; @@ -464,4 +465,4 @@ }; } Validator.$inject = ['$injector']; -}).call(this); +}).call(this); \ No newline at end of file