From 3762f63dae9b646883feabf8089ae9e1560f00e5 Mon Sep 17 00:00:00 2001 From: Olav van Schie Date: Mon, 13 Mar 2017 15:38:26 +0100 Subject: [PATCH] Fix validation if not required --- src/v-validate.js | 55 +++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/v-validate.js b/src/v-validate.js index 2bc07b7..ccde1c0 100644 --- a/src/v-validate.js +++ b/src/v-validate.js @@ -7,7 +7,6 @@ validate.config = {}; var validators = { required: function (value) { - if (typeof value == 'boolean') return value; return !((value == null) || (value.length == 0)); }, @@ -95,6 +94,10 @@ validate.install = function (Vue) { clearTimeout(timers[this.identifier]); } + if (typeof value == 'undefined') { + value = ''; + } + var vm = this; Vue.nextTick(function () { @@ -104,45 +107,51 @@ validate.install = function (Vue) { validate: function (value) { var valid = true; + var isRequired = (typeof this.validateRules['required'] != 'undefined' && this.validateRules['required']); - Object.keys(this.validateRules).forEach(function (rule) { - if (!validators[rule]){ - throw new Error('Unknown rule ' + rule); - } + // if empty, but not required + // it is still valid + if(!isRequired && value.length < 1) { + + } else { + // validate each rule + Object.keys(this.validateRules).forEach(function (rule) { + if (!validators[rule]) { + throw new Error('Unknown rule ' + rule); + } - var ruleArgument = this.validateRules[rule]; + var ruleArgument = this.validateRules[rule]; - // type is boolean and it is true - if (typeof(ruleArgument) == 'boolean' && ruleArgument) { - if(!validators[rule](value)) { + // type is boolean and it is true + if (typeof(ruleArgument) == 'boolean' && ruleArgument && !validators[rule](value)) { return valid = false; - } - } else { - // type has argument - if(!validators[rule](value, ruleArgument)) { + } else if(!validators[rule](value, ruleArgument)) { + // type has argument return valid = false; } - } - }.bind(this)); + }.bind(this)); + } - if (typeof this.vm[errorBag] != "undefined") { + if (typeof this.vm[errorBag] != 'undefined') { Vue.delete(this.vm[errorBag], this.identifier); } + // remove classes + this.el.classList.remove('valid'); + this.el.classList.remove('invalid'); + if (valid) { - // instantly add the valid class - this.el.classList.add('valid'); - this.el.classList.remove('invalid'); + // add the valid class + if(value.length > 0) { + this.el.classList.add('valid'); + } } else { // add the field to the errorbag this.vm.$set(errorBag + '.' + this.identifier, true); - // instantly remove the valid class - this.el.classList.remove('valid'); - // In case of the initial data, only add invalid class // if data is filled - if (!this.isInitial || typeof value != 'undefined') { + if (! (this.isInitial && value.length < 1) ) { var vm = this; // give a sec before adding the invalid class