Skip to content
This repository has been archived by the owner on Dec 25, 2017. It is now read-only.

Commit

Permalink
feat(v-validate): add 'initial' params
Browse files Browse the repository at this point in the history
Closes #58
  • Loading branch information
kazupon committed Mar 12, 2016
1 parent bd1a21b commit 7d1ecd5
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 27 deletions.
13 changes: 11 additions & 2 deletions src/directives/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function (Vue) {

Vue.directive('validate', {
priority: vIf.priority + 1,
params: ['group', 'field', 'detect-blur', 'detect-change'],
params: ['group', 'field', 'detect-blur', 'detect-change', 'initial'],

paramWatchers: {
detectBlur (val, old) {
Expand Down Expand Up @@ -61,7 +61,10 @@ export default function (Vue) {
this.handleArray(value)
}

this.validator.validate(this.field)
this.validator.validate({ field: this.field, noopable: this._initialNoopValidation })
if (this._initialNoopValidation) {
this._initialNoopValidation = null
}
},

unbind () {
Expand All @@ -86,6 +89,8 @@ export default function (Vue) {

params.group
&& validator.addGroupValidation(params.group, this.field)

this._initialNoopValidation = this.isInitialNoopValidation(params.initial)
},

listen () {
Expand Down Expand Up @@ -201,6 +206,10 @@ export default function (Vue) {
isDetectChange (detectChange) {
return detectChange === undefined
|| detectChange === 'on' || detectChange === true
},

isInitialNoopValidation (initial) {
return initial === 'off' || initial === false
}
})
}
7 changes: 6 additions & 1 deletion src/validations/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export default class BaseValidation {
this._validator.validate(this.field)
}

validate (cb) {
validate (cb, noopable = false) {
const _ = util.Vue.util

let results = {}
Expand All @@ -145,6 +145,11 @@ export default class BaseValidation {
msg = descriptor.msg
}

if (noopable) {
results[name] = false
return done()
}

if (validator) {
let value = this._getValue(this._el)
this._invokeValidator(this._vm, validator, value, descriptor.arg, (ret, err) => {
Expand Down
4 changes: 2 additions & 2 deletions src/validations/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class CheckboxValidation extends BaseValidation {
})
}
} else {
this._validator.validate(this.field)
this._validator.validate({ field: this.field })
}
}

Expand All @@ -64,7 +64,7 @@ export default class CheckboxValidation extends BaseValidation {
if (found === -1) { return }

this._inits.splice(found, 1)
this._validator.validate(this.field)
this._validator.validate({ field: this.field })
}

willUpdateFlags () {
Expand Down
4 changes: 2 additions & 2 deletions src/validations/radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class RadioValidation extends BaseValidation {
}
})
} else {
this._validator.validate(this.field)
this._validator.validate({ field: this.field })
}
}

Expand All @@ -44,7 +44,7 @@ export default class RadioValidation extends BaseValidation {
if (found === -1) { return }

this._inits.splice(found, 1)
this._validator.validate(this.field)
this._validator.validate({ field: this.field })
}

willUpdateFlags () {
Expand Down
38 changes: 19 additions & 19 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,21 @@ export default class Validator {

// define the validate manually meta method to vue instance
vm.$validate = (...args) => {
this.validate(...args)
let field = null
let touched = false
let cb = null

each(args, (arg, index) => {
if (typeof arg === 'string') {
field = arg
} else if (typeof arg === 'boolean') {
touched = arg
} else if (typeof arg === 'function') {
cb = arg
}
})

this.validate({ field: field, touched: touched, cb: cb })
}

// define manually the validation errors
Expand Down Expand Up @@ -155,28 +169,14 @@ export default class Validator {
validations && pull(validations, validation)
}

validate (...args) {
let field = null
let touched = false
let cb = null

each(args, (arg, index) => {
if (typeof arg === 'string') {
field = arg
} else if (typeof arg === 'boolean') {
touched = arg
} else if (typeof arg === 'function') {
cb = arg
}
})

validate ({ field = null, touched = false, noopable = false, cb = null } = {}) {
if (!field) { // all
each(this.validations, (validation, key) => {
validation.willUpdateFlags(touched)
})
this._validates(cb)
} else { // each field
this._validate(field, touched, cb)
this._validate(field, touched, noopable, cb)
}
}

Expand All @@ -201,7 +201,7 @@ export default class Validator {
}
}

_validate (field, touched = false, cb = null) {
_validate (field, touched = false, noopable = false, cb = null) {
const scope = this._scope

let validation = this._getValidationFrom(field)
Expand All @@ -211,7 +211,7 @@ export default class Validator {
util.Vue.set(scope, field, results)
this._fireEvents()
cb && cb()
})
}, noopable)
}
}

Expand Down
37 changes: 37 additions & 0 deletions test/specs/directives/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,43 @@ describe('validate directive', () => {


describe('params', () => {
describe('initial', () => {
beforeEach((done) => {
el.innerHTML = '<validator name="validator1">'
+ '<form novalidate>'
+ '<input type="text" initial="off" v-validate:field1="{ required: true, minlength: 5 }">'
+ '</form>'
+ '</validator>'
vm = new Vue({ el: el })
vm.$nextTick(done)
})

it('should not run validation', (done) => {
let field = el.getElementsByTagName('input')[0]

// default
assert(vm.$validator1.field1.required === false) // default validation success
assert(vm.$validator1.field1.minlength === false) // default validation success
assert(vm.$validator1.field1.valid === true)
assert(vm.$validator1.field1.touched === false)
assert(vm.$validator1.field1.modified === false)
assert(vm.$validator1.field1.dirty === false)

// occured blur
trigger(field, 'blur')
vm.$nextTick(() => {
assert(vm.$validator1.field1.required === true)
assert(vm.$validator1.field1.minlength === true)
assert(vm.$validator1.field1.valid === false)
assert(vm.$validator1.field1.touched === true)
assert(vm.$validator1.field1.modified === false)
assert(vm.$validator1.field1.dirty === false)

done()
})
})
})

describe('detect-blur', () => {
beforeEach((done) => {
el.innerHTML = '<validator name="validator1">'
Expand Down
1 change: 0 additions & 1 deletion test/specs/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ describe('event', () => {
input1.value = 'foo'
input2.value = 'bar'
trigger(input1, 'input')
trigger(input2, 'input')
})
})
})
Expand Down

0 comments on commit 7d1ecd5

Please sign in to comment.