From 09894d8bc6a3379cc2741e456d7809a040d1ca49 Mon Sep 17 00:00:00 2001 From: huei90 Date: Sat, 12 Apr 2014 01:35:30 +0800 Subject: [PATCH] Allow disable showing success message #6 --- dist/angular-validation.js | 32 ++++++++++++++++++++++++++++++-- dist/angular-validation.min.js | 2 +- src/directive.js | 12 ++++++++++-- src/provider.js | 20 ++++++++++++++++++++ 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/dist/angular-validation.js b/dist/angular-validation.js index 24544a6..06aa4b3 100644 --- a/dist/angular-validation.js +++ b/dist/angular-validation.js @@ -145,6 +145,24 @@ }; + /** + * Whether show the validation success message + * You can easily change this to false in your config + * example: $validationProvider.showSuccessMessage = false; + * @type {boolean} + */ + this.showSuccessMessage = true; + + + /** + * Whether show the validation error message + * You can easily change this to false in your config + * example: $validationProvider.showErrorMessage = false; + * @type {boolean} + */ + this.showErrorMessage = true; + + /** * Check form valid, return true * checkValid(Form): Check the specific form(Form) valid from angular `$valid` @@ -230,6 +248,8 @@ getExpression: this.getExpression, setDefaultMsg: this.setDefaultMsg, getDefaultMsg: this.getDefaultMsg, + showSuccessMessage: this.showSuccessMessage, + showErrorMessage: this.showErrorMessage, checkValid: this.checkValid, validate: this.validate, reset: this.reset @@ -257,7 +277,11 @@ * @returns {} */ var validFunc = function (element, validMessage, validation, callback, ctrl) { - element.next().html($validationProvider.getSuccessHTML(validMessage || $validationProvider.getDefaultMsg(validation).success)); + if ($validationProvider.showSuccessMessage) { + element.next().html($validationProvider.getSuccessHTML(validMessage || $validationProvider.getDefaultMsg(validation).success)); + } else { + element.next().html(''); + } ctrl.$setValidity(ctrl.$name, true); if (callback) callback(); }; @@ -273,7 +297,11 @@ * @returns {} */ var invalidFunc = function (element, validMessage, validation, callback, ctrl) { - element.next().html($validationProvider.getErrorHTML(validMessage || $validationProvider.getDefaultMsg(validation).error)); + if ($validationProvider.showErrorMessage) { + element.next().html($validationProvider.getErrorHTML(validMessage || $validationProvider.getDefaultMsg(validation).error)); + } else { + element.next().html(''); + } ctrl.$setValidity(ctrl.$name, false); if (callback) callback(); }; diff --git a/dist/angular-validation.min.js b/dist/angular-validation.min.js index da0327e..0fb9f79 100644 --- 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=this,f=function(e){a=e,b=a.get("$rootScope"),c=a.get("$http"),d=a.get("$q")},g={required:/^.+$/,url:/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,email:/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/,number:/^\d+$/},h={required:{error:"This should be Required!!",success:"It's Required"},url:{error:"This should be Url",success:"It's Url"},email:{error:"This should be Email",success:"It's Email"},number:{error:"This should be Number",success:"It's Number"}};this.setExpression=function(a){angular.extend(g,a)},this.getExpression=function(a){return g[a]},this.setDefaultMsg=function(a){angular.extend(h,a)},this.getDefaultMsg=function(a){return h[a]},this.setErrorHTML=function(a){a.constructor===Function&&(e.getErrorHTML=a)},this.getErrorHTML=function(a){return'

'+a+"

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

'+a+"

"},this.checkValid=function(a){return void 0===a.$valid?!1:a&&a.$valid===!0},this.validate=function(a){for(var c in a)a[c].hasOwnProperty("$dirty")&&b.$broadcast(c+"submit");var f=d.defer();return f.promise.success=function(a){return f.promise.then(function(b){a(b)}),f.promise},f.promise.error=function(a){return f.promise.then(null,function(b){a(b)}),f.promise},e.checkValid(a)?f.resolve("success"):f.reject("error"),f.promise},this.reset=function(a){for(var c in a)a[c].hasOwnProperty("$dirty")&&(a[c].$setViewValue(null),a[c].$setPristine(),a[c].$setValidity(a[c].$name,!1),a[c].$render(),b.$broadcast(c+"reset"))},this.$get=["$injector",function(a){return f(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,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){a.next().html(b.getSuccessHTML(c||b.getDefaultMsg(d).success)),f.$setValidity(f.$name,!0),e&&e()},f=function(a,c,d,e,f){a.next().html(b.getErrorHTML(c||b.getDefaultMsg(d).error)),f.$setValidity(f.$name,!1),e&&e()},g=function(a,d,g,h,i,j){var k=i+"SuccessMessage",l=i+"ErrorMessage",m=b.getExpression(i).constructor,n={success:function(){e(d,g[k],i,a.validCallback(),h)},error:function(){f(d,g[l],i,a.invalidCallback(),h)}};return m===Function?c.all([b.getExpression(i)(j)]).then(function(a){return a&&a.length>0&&a[0]?n.success():n.error()},function(){return n.error()}):m===RegExp?b.getExpression(i).test(j)?n.success():n.error():n.error()};return{restrict:"A",require:"ngModel",scope:{model:"=ngModel",validCallback:"&",invalidCallback:"&"},link:function(a,b,c,e){var f=c.validator.split(",");b.after(""),e.$setValidity(e.$name,!1),a.$on(e.$name+"reset",function(){b.next().html("")}),f.forEach(function(d){return a.$on(e.$name+"submit",function(){var f=b[0].value;g(a,b,c,e,d,f)}),"blur"===c.validMethod?void b.bind("blur",function(){var f=b[0].value;a.$apply(function(){g(a,b,c,e,d,f)})}):void("submit"!==c.validMethod&&a.$watch("model",function(f){if(e.$pristine&&e.$viewValue)e.$setViewValue(e.$viewValue);else if(e.$pristine)return void b.next().html("");g(a,b,c,e,d,f)}))}),d(function(){c.$observe("noValidationMessage",function(a){var c=b.next();"true"==a||1==a?c.css("display","none"):("false"==a||0==a)&&c.css("display","block")})})}}}])}.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=this,f=function(e){a=e,b=a.get("$rootScope"),c=a.get("$http"),d=a.get("$q")},g={required:/^.+$/,url:/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,email:/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/,number:/^\d+$/},h={required:{error:"This should be Required!!",success:"It's Required"},url:{error:"This should be Url",success:"It's Url"},email:{error:"This should be Email",success:"It's Email"},number:{error:"This should be Number",success:"It's Number"}};this.setExpression=function(a){angular.extend(g,a)},this.getExpression=function(a){return g[a]},this.setDefaultMsg=function(a){angular.extend(h,a)},this.getDefaultMsg=function(a){return h[a]},this.setErrorHTML=function(a){a.constructor===Function&&(e.getErrorHTML=a)},this.getErrorHTML=function(a){return'

'+a+"

"},this.setSuccessHTML=function(a){a.constructor===Function&&(e.getSuccessHTML=a)},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){for(var c in a)a[c].hasOwnProperty("$dirty")&&b.$broadcast(c+"submit");var f=d.defer();return f.promise.success=function(a){return f.promise.then(function(b){a(b)}),f.promise},f.promise.error=function(a){return f.promise.then(null,function(b){a(b)}),f.promise},e.checkValid(a)?f.resolve("success"):f.reject("error"),f.promise},this.reset=function(a){for(var c in a)a[c].hasOwnProperty("$dirty")&&(a[c].$setViewValue(null),a[c].$setPristine(),a[c].$setValidity(a[c].$name,!1),a[c].$render(),b.$broadcast(c+"reset"))},this.$get=["$injector",function(a){return f(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){a.next().html(b.showSuccessMessage?b.getSuccessHTML(c||b.getDefaultMsg(d).success):""),f.$setValidity(f.$name,!0),e&&e()},f=function(a,c,d,e,f){a.next().html(b.showErrorMessage?b.getErrorHTML(c||b.getDefaultMsg(d).error):""),f.$setValidity(f.$name,!1),e&&e()},g=function(a,d,g,h,i,j){var k=i+"SuccessMessage",l=i+"ErrorMessage",m=b.getExpression(i).constructor,n={success:function(){e(d,g[k],i,a.validCallback(),h)},error:function(){f(d,g[l],i,a.invalidCallback(),h)}};return m===Function?c.all([b.getExpression(i)(j)]).then(function(a){return a&&a.length>0&&a[0]?n.success():n.error()},function(){return n.error()}):m===RegExp&&b.getExpression(i).test(j)?n.success():n.error()};return{restrict:"A",require:"ngModel",scope:{model:"=ngModel",validCallback:"&",invalidCallback:"&"},link:function(a,b,c,e){var f=c.validator.split(",");b.after(""),e.$setValidity(e.$name,!1),a.$on(e.$name+"reset",function(){b.next().html("")}),f.forEach(function(d){return a.$on(e.$name+"submit",function(){var f=b[0].value;g(a,b,c,e,d,f)}),"blur"===c.validMethod?void b.bind("blur",function(){var f=b[0].value;a.$apply(function(){g(a,b,c,e,d,f)})}):void("submit"!==c.validMethod&&a.$watch("model",function(f){if(e.$pristine&&e.$viewValue)e.$setViewValue(e.$viewValue);else if(e.$pristine)return void b.next().html("");g(a,b,c,e,d,f)}))}),d(function(){c.$observe("noValidationMessage",function(a){var c=b.next();"true"==a||1==a?c.css("display","none"):("false"==a||0==a)&&c.css("display","block")})})}}}])}.call(this); \ No newline at end of file diff --git a/src/directive.js b/src/directive.js index 5ffd0ad..9dd46d5 100644 --- a/src/directive.js +++ b/src/directive.js @@ -16,7 +16,11 @@ * @returns {} */ var validFunc = function (element, validMessage, validation, callback, ctrl) { - element.next().html($validationProvider.getSuccessHTML(validMessage || $validationProvider.getDefaultMsg(validation).success)); + if ($validationProvider.showSuccessMessage) { + element.next().html($validationProvider.getSuccessHTML(validMessage || $validationProvider.getDefaultMsg(validation).success)); + } else { + element.next().html(''); + } ctrl.$setValidity(ctrl.$name, true); if (callback) callback(); }; @@ -32,7 +36,11 @@ * @returns {} */ var invalidFunc = function (element, validMessage, validation, callback, ctrl) { - element.next().html($validationProvider.getErrorHTML(validMessage || $validationProvider.getDefaultMsg(validation).error)); + if ($validationProvider.showErrorMessage) { + element.next().html($validationProvider.getErrorHTML(validMessage || $validationProvider.getDefaultMsg(validation).error)); + } else { + element.next().html(''); + } ctrl.$setValidity(ctrl.$name, false); if (callback) callback(); }; diff --git a/src/provider.js b/src/provider.js index 86f1840..b37e6b1 100644 --- a/src/provider.js +++ b/src/provider.js @@ -142,6 +142,24 @@ }; + /** + * Whether show the validation success message + * You can easily change this to false in your config + * example: $validationProvider.showSuccessMessage = false; + * @type {boolean} + */ + this.showSuccessMessage = true; + + + /** + * Whether show the validation error message + * You can easily change this to false in your config + * example: $validationProvider.showErrorMessage = false; + * @type {boolean} + */ + this.showErrorMessage = true; + + /** * Check form valid, return true * checkValid(Form): Check the specific form(Form) valid from angular `$valid` @@ -227,6 +245,8 @@ getExpression: this.getExpression, setDefaultMsg: this.setDefaultMsg, getDefaultMsg: this.getDefaultMsg, + showSuccessMessage: this.showSuccessMessage, + showErrorMessage: this.showErrorMessage, checkValid: this.checkValid, validate: this.validate, reset: this.reset