UNPKG

9.06 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.vueDotnetValidator=e():t.vueDotnetValidator=e()}(window,function(){return function(t){var e={};function i(r){if(e[r])return e[r].exports;var n=e[r]={i:r,l:!1,exports:{}};return t[r].call(n.exports,n,n.exports,i),n.l=!0,n.exports}return i.m=t,i.c=e,i.d=function(t,e,r){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)i.d(r,n,function(e){return t[e]}.bind(null,n));return r},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=0)}([function(t,e,i){"use strict";i.r(e);var r={};i.r(r),i.d(r,"Required",function(){return a}),i.d(r,"MinLength",function(){return o}),i.d(r,"MaxLength",function(){return s}),i.d(r,"Length",function(){return l}),i.d(r,"Range",function(){return u}),i.d(r,"Regex",function(){return d}),i.d(r,"EqualTo",function(){return h}),i.d(r,"IsTrue",function(){return c});var n=function(){function t(t,e,i){this.message=t,this.attributes=e,this.validatorGroup=i}var e=t.prototype;return e.getMessage=function(){return this.message},e.isValid=function(){},t}();var a=function(t){var e,i;function r(){return t.apply(this,arguments)||this}return i=t,(e=r).prototype=Object.create(i.prototype),e.prototype.constructor=e,e.__proto__=i,r.prototype.isValid=function(t){return null!=t&&""!==t},r}(n);var o=function(t){var e,i;function r(e,i){var r;return(r=t.call(this,e,i)||this).minLength=i.valMinlengthMin,r}return i=t,(e=r).prototype=Object.create(i.prototype),e.prototype.constructor=e,e.__proto__=i,r.prototype.isValid=function(t){return!t||t.length>=this.minLength},r}(n);var s=function(t){var e,i;function r(e,i){var r;return(r=t.call(this,e,i)||this).maxLength=i.valMaxlengthMax,r}return i=t,(e=r).prototype=Object.create(i.prototype),e.prototype.constructor=e,e.__proto__=i,r.prototype.isValid=function(t){return!t||t.length<=this.maxLength},r}(n);var l=function(t){var e,i;function r(e,i){var r;return(r=t.call(this,e,i)||this).maxLength=i.valLengthMax,r.minLength=i.valLengthMin,r}return i=t,(e=r).prototype=Object.create(i.prototype),e.prototype.constructor=e,e.__proto__=i,r.prototype.isValid=function(t){return!t||t.length>=this.minLength&&t.length<=this.maxLength},r}(n);var u=function(t){var e,i;function r(e,i){var r;return(r=t.call(this,e,i)||this).min=parseFloat(i.valRangeMin),r.max=parseFloat(i.valRangeMax),r}return i=t,(e=r).prototype=Object.create(i.prototype),e.prototype.constructor=e,e.__proto__=i,r.prototype.isValid=function(t){var e=parseFloat(t);return!t&&0!=e||!isNaN(e)&&e>=this.min&&e<=this.max},r}(n);var d=function(t){var e,i;function r(e,i){var r;return(r=t.call(this,e,i)||this).regex=new RegExp(i.valRegexPattern),r}return i=t,(e=r).prototype=Object.create(i.prototype),e.prototype.constructor=e,e.__proto__=i,r.prototype.isValid=function(t){return!t||this.regex.test(t)},r}(n);var h=function(t){var e,i;function r(e,i,r){var n;return(n=t.call(this,e,i,r)||this).otherFieldName=i.valEqualtoOther,n}return i=t,(e=r).prototype=Object.create(i.prototype),e.prototype.constructor=e,e.__proto__=i,r.prototype.isValid=function(t){var e=this.validatorGroup.findValidatorsByName(this.otherFieldName);if(null==e)return!0;var i=e[0];return!t||!i||t==i.val},r}(n);var c=function(t){var e,i;function r(){return t.apply(this,arguments)||this}return i=t,(e=r).prototype=Object.create(i.prototype),e.prototype.constructor=e,e.__proto__=i,r.prototype.isValid=function(t){return!0===t},r}(n),f=new(function(){function t(){}return t.prototype.findValidatorGroup=function(t){if(t.$parent)return t.$parent.isValidatorGroup?t.$parent:this.findValidatorGroup(t.$parent)},t}());function p(){return(p=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var r in i)Object.prototype.hasOwnProperty.call(i,r)&&(t[r]=i[r])}return t}).apply(this,arguments)}var v=function(t){void 0===t&&(t={});var e=p({},r,{},t),i="after-blur",n="after-change",a="after-submit",o="field-validation-valid",s=null;return{name:"vue-dotnet-validator",props:{value:{default:""},extraErrorMessage:{default:""},validationStyle:{default:i}},data:function(){return{validators:[],blurred:!1,hasValidationError:!1,localInputValue:this.value,isTwoWayBind:!1,hasChanged:!1,hasForced:!1,hasBlurred:!1,name:"",field:null}},updated:function(){this.initialize()},mounted:function(){(s=f.findValidatorGroup(this)).addValidator(this),this.initialize()},destroyed:function(){var t=this;this.$nextTick(function(){s.removeValidator(t)})},methods:{initialize:function(){this.field||(this.field=this.resolveField(this),this.field&&(this.name=this.field.name,this.isTwoWayBind=this.$options._parentListeners&&!!this.$options._parentListeners.input,this.findValidators(),this.addAriaDescribedBy(),this.$refs.message.innerText?(this.hasValidationError=!0,this.blurred=!0):this.$refs.message.classList.add(o),this.isCheckbox||this.isRadio||this.field.addEventListener("blur",this.blurField),this.field.addEventListener("change",this.changeField),this.field.addEventListener("input",this.changeField)))},resolveField:function(t){var e=this;return t?t.$refs.field?t.$refs.field:t.$children.length>0?t.$children.map(function(t){return e.resolveField(t)}).filter(function(t){return!!t})[0]:null:null},blurField:function(t){t&&""!==t.target.value&&(this.val=t.target.value),this.blurred=!0,this.hasBlurred=!0,this.$emit("blur-field",this),this.showValidationMessage(!1)},changeField:function(t){t&&(this.isCheckbox||this.isRadio?(this.blurred=!0,this.val=this.isCheckbox?t.target.checked:t.target.checked?t.target.value:""):this.val=t.target.value),this.hasChanged=!0,this.$emit("change-field",this),this.showValidationMessage(!1)},findValidators:function(){var t=this,i=this.field.dataset;Object.keys(e).forEach(function(r){var n=r.charAt(0).toUpperCase()+r.slice(1).toLowerCase(),a=i["val"+n];a&&t.validators.push(new e[r](a,i,s))})},showValidationMessage:function(t){if(void 0===t&&(t=!1),(t||this.shouldValidate)&&this.$refs.message)return this.$refs.message.innerHTML=this.validationMessage,this.validationMessage?(this.hasValidationError=!0,this.$refs.message.classList.remove(o)):(this.hasValidationError=!1,this.$refs.message.classList.add(o))},addAriaDescribedBy:function(){var t="vue-validator-"+parseInt(100*Math.random())+"-"+this._uid;this.$refs.message.id=t,this.field.setAttribute("aria-describedby",t),this.$refs.message.setAttribute("role","alert")}},computed:{shouldValidate:function(){return!this.field&&this._isMounted&&console.warn("Tring to validate without a field"),!(this.validationStyle===i&&!this.hasBlurred)&&(!(this.validationStyle===a&&!this.hasForced)&&!(this.validationStyle===n&&!this.hasChanged))},isValid:function(){var t=this;return!!this.field&&(this.validators.filter(function(e){return e.isValid(t.val)}).length===this.validators.length&&!this.extraErrorMessage&&!this.hasValidationError)},validationMessage:function(){var t=this;if(!this.field)return"The field was not ready yet, please try again.";var e="";return this.validators.forEach(function(i){i.isValid(t.val)||e||(e=i.getMessage())}),e||this.extraErrorMessage},val:{get:function(){return this.isTwoWayBind?this.value:this.localInputValue},set:function(t){return this.hasChanged=!0,this.isTwoWayBind?this.$emit("input",t):this.localInputValue=t}},isCheckbox:function(){return this.field&&"checkbox"==this.field.type},isRadio:function(){return this.field&&"radio"==this.field.type}},watch:{isValid:function(){this.field&&this.shouldValidate&&this.field.setAttribute("aria-invalid",!this.isValid)},validationMessage:function(){this.showValidationMessage()}}}};var g={name:"vue-dotnet-validator-group",props:{onValid:{type:Function,default:null}},data:function(){return{validators:[],loading:!1,isValidatorGroup:!0}},methods:{validate:function(t){var e=!0;return this.validators.forEach(function(i){i.hasForced=!0,i.isValid||(e=!1,t.preventDefault(),i.showValidationMessage(!0),i.blurField())}),e&&this.onValid instanceof Function?(t.preventDefault(),this.onValid(t)):(this.loading=e,e)},findValidatorsByName:function(t){return t=t.replace(/\*/g,"").replace(/\./g,""),this.validators.filter(function(e){return i=e.name,r=t,-1!==i.indexOf(r,i.length-r.length);var i,r})},addValidator:function(t){this.validators.push(t)},removeValidator:function(t){this.validators.splice(this.validators.indexOf(t),1)}}};i.d(e,"validatorGroup",function(){return g}),i.d(e,"validator",function(){return v}),i.d(e,"BaseValidator",function(){return n})}])});
\No newline at end of file