-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathw5cValidator.min.js
1 lines (1 loc) · 10.1 KB
/
w5cValidator.min.js
1
!function(){var e=angular.module("w5c.validator",["ng"]).provider("w5cValidator",[function(){var e={required:"该选项不能为空",maxlength:"该选项输入值长度不能大于{maxlength}",minlength:"该选项输入值长度不能小于{minlength}",email:"输入邮件的格式不正确",repeat:"两次输入不一致",pattern:"该选项输入格式不正确",number:"必须输入数字",w5cuniquecheck:"该输入值已经存在,请重新输入",url:"输入URL格式不正确",max:"该选项输入值不能大于{max}",min:"该选项输入值不能小于{min}",customizer:"自定义验证不通过"},r=["text","password","email","number","url","tel","hidden",["textarea"],["select"],["select-multiple"],["select-one"],"radio","checkbox"],t=function(e){return"FORM"===e[0].tagName||11==e[0].nodeType?null:e&&e.hasClass("form-group")?e:t(e.parent())},n=function(){this.elemTypes=r,this.rules={},this.isEmpty=function(e){return!e||e instanceof Array&&0===e.length},this.defaultShowError=function(e,r){var n=angular.element(e),i=t(n);this.isEmpty(i)||i.hasClass("has-error")||i.addClass("has-error");var o=n.next();o&&o.hasClass("w5c-error")||n.after('<span class="w5c-error">'+r[0]+"</span>")},this.defaultRemoveError=function(e){var r=angular.element(e),n=t(r);!this.isEmpty(n)&&n.hasClass("has-error")&&n.removeClass("has-error");var i=r.next();i.hasClass&&i.hasClass("w5c-error")&&i.remove()},this.options={blurTrig:!1,showError:!0,removeError:!0}};n.prototype={constructor:n,config:function(e){this.options=angular.extend(this.options,e)},setRules:function(e){this.rules=angular.extend(this.rules,e)},getErrorMessage:function(r,t){var n=null,i=t.name;switch(i&&/\$\d+\$/i.test(i)&&(i=i.replace(/\$\d+\$/i,"")),this.isEmpty(this.rules[i])||this.isEmpty(this.rules[i][r])||(n=this.rules[i][r]),r){case"maxlength":return(n||e.maxlength).replace("{maxlength}",t.getAttribute("ng-maxlength"));case"minlength":return(n||e.minlength).replace("{minlength}",t.getAttribute("ng-minlength"));case"max":return(n||e.max).replace("{max}",t.getAttribute("max"));case"min":return(n||e.min).replace("{min}",t.getAttribute("min"));default:if(null!==n)return n;if(null===e[r])throw new Error("该验证规则("+r+")默认错误信息没有设置!");return e[r]}},getErrorMessages:function(e,r){var t=[];for(var n in r)if(r[n]){var i=this.getErrorMessage(n,e);t.push(i)}return t},showError:function(e,r,t){var n=angular.extend({},this.options,t);if(angular.element(e).removeClass("valid").addClass("error"),!1!==n.showError)return angular.isFunction(n.showError)?n.showError(e,r):!0===n.showError?this.defaultShowError(e,r):void 0},removeError:function(e,r){var t=angular.extend({},this.options,r);if(angular.element(e).removeClass("error").addClass("valid"),!1!==t.removeError)return angular.isFunction(t.removeError)?t.removeError(e):!0===t.removeError?this.defaultRemoveError(e):void 0}};var i=new n;this.config=function(e){i.config(e)},this.setRules=function(e){i.setRules(e)},this.setDefaultRules=function(r){e=angular.extend(e,r)},this.$get=function(){return i}}]);"undefined"!=typeof module&&"undefined"!=typeof exports&&module.exports===exports&&(module.exports=e.name)}(),function(){function e(e,r,t,n,i,o){var a=this,s=r[0];this.formCtrl=null,this.needBindKeydown=!1,this.enterKeydownFn=null,this.validElements=[],this.setElementErrorMessage=function(e,r){var t=this,i=s[e];e&&t.validElements.indexOf(e)>=0&&(n.removeError(i,t.options),n.showError(i,[r],t.options),t.formCtrl.$errors.push(r))},this.validateFormElement=function(e){var r=this,t=s[e];if(!n.isEmpty(e)&&t&&r.formCtrl[e]&&n.elemTypes.toString().indexOf(t.type)>-1)if(r.validElements.indexOf(e)>=0){if(!r.formCtrl[e].$valid){var i=n.getErrorMessages(t,r.formCtrl[t.name].$error);return n.removeError(t,r.options),r.formCtrl[e].w5cError=!0,i&&i.length>0&&(n.showError(t,i,r.options),r.formCtrl.$errors.push(i[0])),i}n.removeError(t,r.options),r.formCtrl[e].w5cError=!1}else if(!r.formCtrl[e].$valid){i=n.getErrorMessages(t,r.formCtrl[t.name].$error);if(n.removeError(t,r.options),r.formCtrl[e].w5cError=!1,i&&i.length>0){var o=r.formCtrl.$errors.indexOf(i[0]);o>=0&&r.formCtrl.$errors.splice(o,1)}}return null},this.validateForm=function(){for(var r=this.formCtrl,t=[],i=0;i<s.elements.length;i++){var o=this.validateFormElement(s.elements[i].name);o&&o.length>=1&&t.push(o[0])}!n.isEmpty(t)&&t.length>0?r.$errors=t:r.$errors=[],e.$$phase||e.$apply(r.$errors)},this.setValidate=function(l){this.formCtrl=l;var u=l.doValidate=this.validateForm.bind(this);l.validateElement=this.validateFormElement.bind(this),l.setElementErrorMessage=this.setElementErrorMessage.bind(this),l.reset=function(){o(function(){l.$setPristine();for(var e=0;e<s.elements.length;e++){var r=s.elements[e],t=angular.element(r);n.removeError(t,a.options)}l.$errors=[]})},l.w5cValidator={doValidate:this.validateForm.bind(this),validateElement:this.validateFormElement.bind(this),setElementErrorMessage:this.setElementErrorMessage.bind(this),removeElementValidation:this.removeElementValidation.bind(this),addElementValidation:this.addElementValidation.bind(this),formIsValid:this.formIsValid.bind(this)};var m=i(t.w5cSubmit);t.w5cSubmit&&angular.isFunction(m)&&(r.bind("submit",function(r){u(),l.w5cValidator.formIsValid()&&angular.isFunction(m)&&e.$apply(function(){m(e,{$event:r})})}),this.needBindKeydown=!0),this.needBindKeydown&&r.bind("keydown keypress",function(e){if(13===e.which){var t=document.activeElement;if(t.type&&"textarea"!==t.type){var n=r.find("button");n&&n[0]&&n[0].focus(),t.focus(),e.preventDefault(),angular.isFunction(a.enterKeydownFn)&&a.enterKeydownFn(e)}}})},this.formIsValid=function(){var e=this.formCtrl,r=!0;return this.validElements.forEach(function(t){e[t]&&e[t].$invalid&&(r=!1)}),r},this.doValidate=function(r,t){var n=this;if(angular.isFunction(n.formCtrl.doValidate)&&n.formCtrl.doValidate(),this.formIsValid()&&angular.isFunction(r))e.$apply(function(){r()});else if(angular.isFunction(t)){var i=n._getInvalidElements();e.$apply(function(){t(n.formCtrl.$errors,i)})}},this._getInvalidElements=function(){for(var e=[],t=0;t<r[0].elements.length;t++){var n=r[0].elements[t];this.validElements.indexOf(n.name)>=0&&this.formCtrl[n.name]&&this.formCtrl[n.name].$invalid&&e.push(n)}return e},this.removeElementValidation=function(e){var r=this.validElements.indexOf(e);r>=0&&(this.validElements.splice(r,1),this.validateFormElement(e))},this.addElementValidation=function(e){this.validElements.indexOf(e)<0&&s[e]&&this.initElement(s[e])},this.removeError=function(e){this.formCtrl.$errors=[],this.formCtrl[e[0].name]&&(this.formCtrl[e[0].name].w5cError=!1),n.removeError(e,this.options)},this.initElement=function(r){var t=angular.element(r),i=this;if(n.elemTypes.toString().indexOf(r.type)>-1&&!n.isEmpty(r.name)&&!/^\d/.test(r.name)){var a=t.attr("disabled");if(a&&("true"===a||"disabled"===a))return;if(!(this.validElements.indexOf(r.name)<0))return;this.validElements.push(r.name);var s=this.formName+"."+r.name+".$viewValue";e.$watch(s,function(){i.removeError(t)},!0),i.options&&i.options.blurTrig&&t.bind("blur",function(){if(i.options.blurTrig){var e=this;o(function(){i.validateFormElement(e.name)},50)}})}},this._init=function(){if(this.formName=r.attr("name"),!this.formName)throw Error("form must has name when use w5cFormValidate");var i=e.$eval(t.w5cFormValidate);t.w5cFormValidate&&e.$watch(t.w5cFormValidate,function(e){e&&(a.options=i=angular.extend({},n.options,e))},!0),this.options=angular.extend({},n.options,i);for(var o=0;o<r[0].elements.length;o++)this.initElement(r[0].elements[o])},this._init()}angular.module("w5c.validator").directive("w5cFormValidate",[function(){return{require:["w5cFormValidate","^?form"],controller:["$scope","$element","$attrs","w5cValidator","$parse","$timeout",e],link:function(e,r,t,n){var i=n[0],o=n[1];i.setValidate(o)}}}]).directive("w5cFormSubmit",["$parse",function(e){return{require:"^w5cFormValidate",link:function(r,t,n,i){var o=e(n.w5cFormSubmit),a=e(n.errorCallback),s=function(e){i.doValidate(function(){o(r,{$event:e})},function(t,n){a&&a(r,{$event:e,$errors:t,$invalidElements:n})})};t.bind("click",s),i.needBindKeydown=!0,i.enterKeydownFn=s}}}]).directive("w5cRepeat",["$timeout",function(e){"use strict";return{require:["ngModel","^w5cFormValidate"],link:function(r,t,n,i){e(function(){var e=t.inheritedData("$formController")[n.w5cRepeat],r=i[0],o=i[1];r.$parsers.push(function(t){return t===e.$modelValue?r.$setValidity("repeat",!0):r.$setValidity("repeat",!1),t}),e.$parsers.push(function(e){return r.$setValidity("repeat",e===r.$modelValue),e===r.$modelValue&&o.removeError(t),e})})}}}]).directive("w5cCustomizer",["$timeout",function(e){"use strict";return{require:["^form","?ngModel"],link:function(e,r,t,n){var i=n[1],o=function(){!0===e.$eval(t.w5cCustomizer)?i.$setValidity("customizer",!0):i.$setValidity("customizer",!1)},a=n[0][t.associate];a&&a.$viewChangeListeners.push(o),i.$viewChangeListeners.push(o),o()}}}]).directive("w5cUniqueCheck",["$timeout","$http","w5cValidator",function(e,r,t){return{require:["ngModel","?^w5cFormValidate","?^form"],link:function(e,n,i,o){var a=o[0],s=o[1],l=o[2],u=function(){var o=e.$eval(i.w5cUniqueCheck),u=o.url,m=o.isExists;r.get(u).success(function(e){var r=!1===m?"true"==e||1==e:!("true"==e||1==e);if(a.$setValidity("w5cuniquecheck",r),!r){var i=t.getErrorMessage("w5cuniquecheck",n[0]);t.showError(n[0],[i],s.options),l[n[0].name]&&(l[n[0].name].w5cError=!0),l.$errors?l.$errors.unshift(i):l.$errors=[i]}})};if(a.$viewChangeListeners.push(function(){l.$errors=[],a.$setValidity("w5cuniquecheck",!0),a.$invalid&&!a.$error.w5cuniquecheck||a.$dirty&&u()}),e.$eval(i.ngModel)){if(a.$invalid&&!a.$error.w5cuniquecheck)return;u()}}}}]).directive("w5cDynamicName",[function(){return{restrict:"A",require:"?ngModel",link:function(e,r,t,n){var i=e.$eval(t.w5cDynamicName)||t.w5cDynamicName;i&&(n.$name=i,r.attr("name",i),(r.controller("form")||{$addControl:angular.noop}).$addControl(n))}}}]).directive("w5cDynamicElement",["$timeout",function(e){return{restrict:"A",require:["?ngModel","?^w5cFormValidate","?^form"],link:function(r,t,n,i){var o=t[0].name,a=i[2];if(o){r.$on("$destroy",function(e){r[a.$name]&&i[1].removeElementValidation(o)}),a[o]||a.$addControl(i[0]);var s=!1;i[2].$errors&&i[2].$errors.length>0&&(s=!0),i[1].initElement(t[0]),s&&e(function(){i[1].doValidate(angular.noop)})}}}}])}();