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

Commit

Permalink
feat(custom): support global error message
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Dec 21, 2015
1 parent 661b60a commit c75480f
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 27 deletions.
46 changes: 34 additions & 12 deletions src/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,44 @@ export default class Validation {
}

validate () {
const extend = util.Vue.util.extend
let ret = Object.create(null)
const _ = util.Vue.util

let results = Object.create(null)
let messages = Object.create(null)
let valid = true

each(this.validators, (descriptor, name) => {
let validator = this._resolveValidator(name)
let res = validator.call(this.dir.vm, this.el.value, descriptor.arg)
if (!res) {
valid = false
let msg = descriptor.msg
if (msg) {
messages[name] = typeof msg === 'function' ? msg() : msg
let asset = this._resolveValidator(name)
let validator = null
let msg = null

if (_.isPlainObject(asset)) {
if (asset.check && typeof asset.check === 'function') {
validator = asset.check
}
if (asset.message) {
msg = asset.message
}
} else if (typeof asset === 'function') {
validator = asset
}

if (descriptor.msg) {
msg = descriptor.msg
}

if (validator) {
let ret = validator.call(this.dir.vm, this.el.value, descriptor.arg)
if (!ret) {
valid = false
if (msg) {
messages[name] = typeof msg === 'function'
? msg.call(this.dir.vm, this.model, descriptor.arg)
: msg
}
}
results[name] = !ret
}
ret[name] = !res
}, this)

trigger(this.el, valid ? 'valid' : 'invalid')
Expand All @@ -85,9 +107,9 @@ export default class Validation {
if (!empty(messages)) {
props.messages = messages
}
extend(ret, props)
_.extend(results, props)

return ret
return results
}

_resolveValidator (name) {
Expand Down
125 changes: 116 additions & 9 deletions test/specs/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,125 @@ describe('custom', () => {
})


describe('caml-case', () => {
it.skip('should be validated', (done) => {
// TODO:
done()
describe('with message', () => {
context('string', () => {
beforeEach((done) => {
Vue.validator('numeric', {
message: 'invalid numeric value',
check (val) {
return /^[-+]?[0-9]+$/.test(val)
}
})

el.innerHTML =
'<validator name="validator1">' +
'<form novalidate>' +
'<input type="text" value="hi" v-validate:field1="[\'numeric\']">' +
'</form>' +
'</validator>'
vm = new Vue({
el: el
})
vm.$nextTick(done)
})

it('should be validated', (done) => {
assert(vm.$validator1.field1.numeric === true)
assert(vm.$validator1.field1.messages.numeric === 'invalid numeric value')

let input = el.getElementsByTagName('input')[0]
input.value = '10'
trigger(input, 'input')
vm.$nextTick(() => {
assert(vm.$validator1.field1.numeric === false)
assert(vm.$validator1.field1.messages === undefined)
done()
})
})
})
})

context('function', () => {
beforeEach((done) => {
Vue.validator('numeric', {
message (field) {
return this.format + field
},
check (val) {
return /^[-+]?[0-9]+$/.test(val)
}
})

el.innerHTML =
'<validator name="validator1">' +
'<form novalidate>' +
'<input type="text" value="hi" v-validate:field1="[\'numeric\']">' +
'</form>' +
'</validator>'
vm = new Vue({
el: el,
data: {
format: 'invalid numeric value : '
}
})
vm.$nextTick(done)
})

describe('kebab-case', () => {
it.skip('should be validated', (done) => {
// TODO:
done()
it('should be validated', (done) => {
assert(vm.$validator1.field1.numeric === true)
assert(vm.$validator1.field1.messages.numeric === (vm.format + 'field1'))

let input = el.getElementsByTagName('input')[0]
input.value = '10'
trigger(input, 'input')
vm.$nextTick(() => {
assert(vm.$validator1.field1.numeric === false)
assert(vm.$validator1.field1.messages === undefined)
done()
})
})
})

context('build-in', () => {
let org
beforeEach((done) => {
let org = Vue.validator('required')
let required = {
message (field) {
return 'required ' + field
},
}
required.check = org
Vue.validator('required', required)

el.innerHTML =
'<validator name="validator1">' +
'<form novalidate>' +
'<input type="text" value="" v-validate:field1="[\'required\']">' +
'</form>' +
'</validator>'
vm = new Vue({
el: el
})
vm.$nextTick(done)
})

afterEach(() => {
Vue.validator('required', org)
})

it('should be validated', (done) => {
assert(vm.$validator1.field1.required === true)
assert(vm.$validator1.field1.messages.required === 'required field1')

let input = el.getElementsByTagName('input')[0]
input.value = '10'
trigger(input, 'input')
vm.$nextTick(() => {
assert(vm.$validator1.field1.required === false)
assert(vm.$validator1.field1.messages === undefined)
done()
})
})
})
})
})
12 changes: 6 additions & 6 deletions test/specs/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('messages', () => {
'<input type="text" group="group2" v-validate:field3="field3">' +
'<input type="text" group="group2" v-validate:field4="field4">' +
'<input type="text" group="group1" value="0" v-validate:field5="{ min: { rule :1, message: message1 } }">' +
'<input type="text" group="group2" value="h" v-validate:field6="{ minlength: { rule: 4, message: onMessage2 } }">' +
'<input type="text" group="group2" value="foo" v-validate:field6="{ minlength: { rule: 4, message: onMessage2 } }">' +
'<ul><li v-for="msg in $validation.messages">' +
'<div v-for="val in msg"><p>{{$key}}:{{val}}</p></div>' +
'</li></ul>' +
Expand All @@ -34,8 +34,8 @@ describe('messages', () => {
}
},
methods: {
onMessage2 () {
return 'field6 short too'
onMessage2 (field) {
return 'field6 short too ' + field
}
}
})
Expand Down Expand Up @@ -64,7 +64,7 @@ describe('messages', () => {
assert(vm.$validation.field3.messages.max === vm.field3.max.message)
assert(vm.$validation.field4.messages.maxlength === vm.field4.maxlength.message)
assert(vm.$validation.field5.messages.min === vm.message1)
assert(vm.$validation.field6.messages.minlength === vm.onMessage2())
assert(vm.$validation.field6.messages.minlength === vm.onMessage2('field6'))
})
})

Expand All @@ -75,7 +75,7 @@ describe('messages', () => {
assert(vm.$validation.messages.field3.max === vm.field3.max.message)
assert(vm.$validation.messages.field4.maxlength === vm.field4.maxlength.message)
assert(vm.$validation.messages.field5.min === vm.message1)
assert(vm.$validation.messages.field6.minlength === vm.onMessage2())
assert(vm.$validation.messages.field6.minlength === vm.onMessage2('field6'))
})
})

Expand All @@ -86,7 +86,7 @@ describe('messages', () => {
assert(vm.$validation.group1.messages.field5.min === vm.message1)
assert(vm.$validation.group2.messages.field3.max === vm.field3.max.message)
assert(vm.$validation.group2.messages.field4.maxlength === vm.field4.maxlength.message)
assert(vm.$validation.group2.messages.field6.minlength === vm.onMessage2())
assert(vm.$validation.group2.messages.field6.minlength === vm.onMessage2('field6'))
})
})
})
Expand Down

0 comments on commit c75480f

Please sign in to comment.