From 53bad6acc090b5ad6cd2b927ed3bb53dc9edcf64 Mon Sep 17 00:00:00 2001 From: "U-US\\katerbm" Date: Wed, 19 Nov 2014 14:00:44 -0600 Subject: [PATCH 1/2] Prevents span from being displayed for valid input without messages --- src/directive.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/directive.js b/src/directive.js index e8f24de..5a7de85 100644 --- a/src/directive.js +++ b/src/directive.js @@ -17,10 +17,14 @@ * @returns {} */ var validFunc = function(element, validMessage, validation, callback, ctrl) { - if ($validationProvider.showSuccessMessage) { - element.next().html($validationProvider.getSuccessHTML(validMessage || $validationProvider.getDefaultMsg(validation).success)); + var messageElem = element.next(), + messageToShow = validMessage || $validationProvider.getDefaultMsg(validation).success; + + if ($validationProvider.showSuccessMessage && messageToShow) { + messageElem.html($validationProvider.getSuccessHTML(messageToShow)); + messageElem.css('display', ''); } else { - element.next().html(''); + messageElem.css('display', 'none'); } ctrl.$setValidity(ctrl.$name, true); if (callback) callback(); @@ -39,10 +43,12 @@ * @returns {} */ var invalidFunc = function(element, validMessage, validation, callback, ctrl) { + var messageElem = element.next(); + messageElem.css('display', ''); if ($validationProvider.showErrorMessage) { - element.next().html($validationProvider.getErrorHTML(validMessage || $validationProvider.getDefaultMsg(validation).error)); + messageElem.html($validationProvider.getErrorHTML(validMessage || $validationProvider.getDefaultMsg(validation).error)); } else { - element.next().html(''); + messageElem.html(''); } ctrl.$setValidity(ctrl.$name, false); if (callback) callback(); @@ -174,7 +180,7 @@ /** * Set initial validity to undefined if no boolean value is transmitted */ - var initialValidity = undefined; + var initialValidity; if (typeof scope.initialValidity === 'boolean') { initialValidity = scope.initialValidity; } From e6bd4c3d1ec38fd765183cbf8167574fb7a57d1c Mon Sep 17 00:00:00 2001 From: "U-US\\katerbm" Date: Thu, 20 Nov 2014 08:40:21 -0600 Subject: [PATCH 2/2] Updates dist files --- dist/angular-validation-rule.js | 0 dist/angular-validation-rule.min.js | 0 dist/angular-validation.js | 20 +++++++++++++------- dist/angular-validation.min.js | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) mode change 100644 => 100755 dist/angular-validation-rule.js mode change 100644 => 100755 dist/angular-validation-rule.min.js mode change 100644 => 100755 dist/angular-validation.js mode change 100644 => 100755 dist/angular-validation.min.js diff --git a/dist/angular-validation-rule.js b/dist/angular-validation-rule.js old mode 100644 new mode 100755 diff --git a/dist/angular-validation-rule.min.js b/dist/angular-validation-rule.min.js old mode 100644 new mode 100755 diff --git a/dist/angular-validation.js b/dist/angular-validation.js old mode 100644 new mode 100755 index df1ce4f..637df7c --- a/dist/angular-validation.js +++ b/dist/angular-validation.js @@ -277,7 +277,7 @@ }); }).call(this); - + (function() { angular.module('validation.directive', ['validation.provider']) .directive('validator', ['$injector', @@ -297,10 +297,14 @@ * @returns {} */ var validFunc = function(element, validMessage, validation, callback, ctrl) { - if ($validationProvider.showSuccessMessage) { - element.next().html($validationProvider.getSuccessHTML(validMessage || $validationProvider.getDefaultMsg(validation).success)); + var messageElem = element.next(), + messageToShow = validMessage || $validationProvider.getDefaultMsg(validation).success; + + if ($validationProvider.showSuccessMessage && messageToShow) { + messageElem.html($validationProvider.getSuccessHTML(messageToShow)); + messageElem.css('display', ''); } else { - element.next().html(''); + messageElem.css('display', 'none'); } ctrl.$setValidity(ctrl.$name, true); if (callback) callback(); @@ -319,10 +323,12 @@ * @returns {} */ var invalidFunc = function(element, validMessage, validation, callback, ctrl) { + var messageElem = element.next(); + messageElem.css('display', ''); if ($validationProvider.showErrorMessage) { - element.next().html($validationProvider.getErrorHTML(validMessage || $validationProvider.getDefaultMsg(validation).error)); + messageElem.html($validationProvider.getErrorHTML(validMessage || $validationProvider.getDefaultMsg(validation).error)); } else { - element.next().html(''); + messageElem.html(''); } ctrl.$setValidity(ctrl.$name, false); if (callback) callback(); @@ -454,7 +460,7 @@ /** * Set initial validity to undefined if no boolean value is transmitted */ - var initialValidity = undefined; + var initialValidity; if (typeof scope.initialValidity === 'boolean') { initialValidity = scope.initialValidity; } diff --git a/dist/angular-validation.min.js b/dist/angular-validation.min.js old mode 100644 new mode 100755 index 79cecbb..89ea56a --- a/dist/angular-validation.min.js +++ b/dist/angular-validation.min.js @@ -1 +1 @@ -(function(){angular.module("validation",["validation.provider","validation.directive"])}).call(this),function(){angular.module("validation.provider",[]).provider("$validation",function(){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={};this.setExpression=function(a){return angular.extend(h,a),f},this.getExpression=function(a){return h[a]},this.setDefaultMsg=function(a){return angular.extend(i,a),f},this.getDefaultMsg=function(a){return i[a]},this.setErrorHTML=function(a){return a.constructor===Function?(f.getErrorHTML=a,f):void 0},this.getErrorHTML=function(a){return'

'+a+"

"},this.setSuccessHTML=function(a){return a.constructor===Function?(f.getSuccessHTML=a,f):void 0},this.getSuccessHTML=function(a){return'

'+a+"

"},this.showSuccessMessage=!0,this.showErrorMessage=!0,this.checkValid=function(a){return void 0===a.$valid?!1:a&&a.$valid===!0},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.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.$get=["$injector",function(a){return g(a),{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,reset:this.reset}}]})}.call(this),function(){angular.module("validation.directive",["validation.provider"]).directive("validator",["$injector",function(a){var b=a.get("$validation"),c=a.get("$q"),d=a.get("$timeout"),e=function(a,c,d,e,f){return a.next().html(b.showSuccessMessage?b.getSuccessHTML(c||b.getDefaultMsg(d).success):""),f.$setValidity(f.$name,!0),e&&e(),!0},f=function(a,c,d,e,f){return a.next().html(b.showErrorMessage?b.getErrorHTML(c||b.getDefaultMsg(d).error):""),f.$setValidity(f.$name,!1),e&&e(),!1},g=!1,h=function(a,d,g,i,j,k){var l=j.slice(0),m=l[0].trim(),n=l.slice(1),o=m+"SuccessMessage",p=m+"ErrorMessage",q=b.getExpression(m),r={success:function(){return e(d,g[o],m,a.validCallback,i),n.length?void h(a,d,g,i,n,k):!0},error:function(){return f(d,g[p],m,a.invalidCallback,i)}};if(void 0===q){if(console.error('You are using undefined validator "%s"',m),!n.length)return;h(a,d,g,i,n,k)}return q.constructor===Function?c.all([b.getExpression(m)(k,a,d,g)]).then(function(a){return a&&a.length>0&&a[0]?r.success():r.error()},function(){return r.error()}):q.constructor===RegExp&&b.getExpression(m).test(k)?r.success():r.error()},i=function(){return(65536*(1+Math.random())|0).toString(16).substring(1)},j=function(){return i()+i()+i()+i()};return{restrict:"A",require:"ngModel",scope:{model:"=ngModel",initialValidity:"=initialValidity",validCallback:"&",invalidCallback:"&"},link:function(a,b,c,e){var f=function(){},i=c.validator.split(","),k=e.validationId=j();b.after("");var l=void 0;"boolean"==typeof a.initialValidity&&(l=a.initialValidity),e.$setValidity(e.$name,l),a.$on(e.$name+"reset-"+k,function(){f(),g=!1,e.$setViewValue(""),e.$setPristine(),e.$setValidity(e.$name,void 0),e.$render(),b.next().html("")}),function(){return a.$on(e.$name+"submit-"+k,function(d,j){var k=e.$viewValue,l=!1;0===j&&(g=!1),l=h(a,b,c,e,i,k),"submit"===c.validMethod&&(f(),f=a.$watch("model",function(d,f){d!==f&&((void 0===d||null===d)&&(d=""),l=h(a,b,c,e,i,d))})),g||l||(g=!0,b[0].focus())}),"blur"===c.validMethod?void b.bind("blur",function(){var d=e.$viewValue;a.$apply(function(){h(a,b,c,e,i,d)})}):void("submit"!==c.validMethod&&"submit-only"!==c.validMethod&&a.$watch("model",function(d){if(e.$pristine&&e.$viewValue)e.$setViewValue(e.$viewValue);else if(e.$pristine)return void b.next().html("");h(a,b,c,e,i,d)}))}(),d(function(){c.$observe("noValidationMessage",function(a){var c=b.next();"true"==a||a===!0?c.css("display","none"):("false"==a||a===!1)&&c.css("display","block")})})}}}]).directive("validationSubmit",["$injector",function(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)})})})}}}]).directive("validationReset",["$injector",function(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)})})}}}])}.call(this); \ No newline at end of file +(function(){angular.module("validation",["validation.provider","validation.directive"])}).call(this),function(){angular.module("validation.provider",[]).provider("$validation",function(){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={};this.setExpression=function(a){return angular.extend(h,a),f},this.getExpression=function(a){return h[a]},this.setDefaultMsg=function(a){return angular.extend(i,a),f},this.getDefaultMsg=function(a){return i[a]},this.setErrorHTML=function(a){return a.constructor===Function?(f.getErrorHTML=a,f):void 0},this.getErrorHTML=function(a){return'

'+a+"

"},this.setSuccessHTML=function(a){return a.constructor===Function?(f.getSuccessHTML=a,f):void 0},this.getSuccessHTML=function(a){return'

'+a+"

"},this.showSuccessMessage=!0,this.showErrorMessage=!0,this.checkValid=function(a){return void 0===a.$valid?!1:a&&a.$valid===!0},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.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.$get=["$injector",function(a){return g(a),{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,reset:this.reset}}]})}.call(this),function(){angular.module("validation.directive",["validation.provider"]).directive("validator",["$injector",function(a){var b=a.get("$validation"),c=a.get("$q"),d=a.get("$timeout"),e=function(a,c,d,e,f){var g=a.next(),h=c||b.getDefaultMsg(d).success;return b.showSuccessMessage&&h?(g.html(b.getSuccessHTML(h)),g.css("display","")):g.css("display","none"),f.$setValidity(f.$name,!0),e&&e(),!0},f=function(a,c,d,e,f){var g=a.next();return g.css("display",""),g.html(b.showErrorMessage?b.getErrorHTML(c||b.getDefaultMsg(d).error):""),f.$setValidity(f.$name,!1),e&&e(),!1},g=!1,h=function(a,d,g,i,j,k){var l=j.slice(0),m=l[0].trim(),n=l.slice(1),o=m+"SuccessMessage",p=m+"ErrorMessage",q=b.getExpression(m),r={success:function(){return e(d,g[o],m,a.validCallback,i),n.length?void h(a,d,g,i,n,k):!0},error:function(){return f(d,g[p],m,a.invalidCallback,i)}};if(void 0===q){if(console.error('You are using undefined validator "%s"',m),!n.length)return;h(a,d,g,i,n,k)}return q.constructor===Function?c.all([b.getExpression(m)(k,a,d,g)]).then(function(a){return a&&a.length>0&&a[0]?r.success():r.error()},function(){return r.error()}):q.constructor===RegExp&&b.getExpression(m).test(k)?r.success():r.error()},i=function(){return(65536*(1+Math.random())|0).toString(16).substring(1)},j=function(){return i()+i()+i()+i()};return{restrict:"A",require:"ngModel",scope:{model:"=ngModel",initialValidity:"=initialValidity",validCallback:"&",invalidCallback:"&"},link:function(a,b,c,e){var f=function(){},i=c.validator.split(","),k=e.validationId=j();b.after("");var l;"boolean"==typeof a.initialValidity&&(l=a.initialValidity),e.$setValidity(e.$name,l),a.$on(e.$name+"reset-"+k,function(){f(),g=!1,e.$setViewValue(""),e.$setPristine(),e.$setValidity(e.$name,void 0),e.$render(),b.next().html("")}),function(){return a.$on(e.$name+"submit-"+k,function(d,j){var k=e.$viewValue,l=!1;0===j&&(g=!1),l=h(a,b,c,e,i,k),"submit"===c.validMethod&&(f(),f=a.$watch("model",function(d,f){d!==f&&((void 0===d||null===d)&&(d=""),l=h(a,b,c,e,i,d))})),g||l||(g=!0,b[0].focus())}),"blur"===c.validMethod?void b.bind("blur",function(){var d=e.$viewValue;a.$apply(function(){h(a,b,c,e,i,d)})}):void("submit"!==c.validMethod&&"submit-only"!==c.validMethod&&a.$watch("model",function(d){if(e.$pristine&&e.$viewValue)e.$setViewValue(e.$viewValue);else if(e.$pristine)return void b.next().html("");h(a,b,c,e,i,d)}))}(),d(function(){c.$observe("noValidationMessage",function(a){var c=b.next();"true"==a||a===!0?c.css("display","none"):("false"==a||a===!1)&&c.css("display","block")})})}}}]).directive("validationSubmit",["$injector",function(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)})})})}}}]).directive("validationReset",["$injector",function(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)})})}}}])}.call(this); \ No newline at end of file