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

Commit

Permalink
feat(validator): change validation result of validator
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Feb 21, 2016
1 parent 5af4efa commit d82dd5e
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 30 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Validation results can be accessed in this structure:
field1: {
required: false, // validator
email: true, // custom validator
url: 'invalid url format', // if specify the error message, set it
...
customValidator1: false,

Expand Down Expand Up @@ -557,7 +558,30 @@ The vue binding syntax can group inputs together:

# Error messages

Error messages can be stored directly in the validation rules, rather than relying on `v-show` or `v-if`:
Error messages can be stored directly in the validation rules, and can be use error message on `v-show` or `v-if`:

```html
<validator name="validation1">
<div class="username">
<label for="username">username:</label>
<input id="username" type="text" v-validate:username="{
required: { rule: true, message: 'required you name !!' }
}">
<span v-if="$validation1.username.required">{{ $validation1.username.required }}</span>
</div>
<div class="password">
<label for="password">password:</label>
<input id="password" type="text" v-validate:password="{
required: { rule: true, message: 'required you password !!' },
minlength: { rule: 8, message: 'your password short too !!' }
}"/>
<span v-if="$validation1.password.required">{{ $validation1.password.required }}</span>
<span v-if="$validation1.password.minlength">{{ $validation1.password.minlength }}</span>
</div>
</validator>
```

Also, Error message can be used with `v-for`:

```html
<validator name="validation1">
Expand Down
6 changes: 5 additions & 1 deletion src/validations/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,13 @@ export default class BaseValidation {
? msg.call(this._vm, this.field, descriptor.arg)
: msg
errors.push(error)
results[name] = error.message
} else {
results[name] = !ret
}
} else {
results[name] = !ret
}
results[name] = !ret
}
}, this)

Expand Down
14 changes: 7 additions & 7 deletions test/specs/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('checkbox', () => {

it('should be validated', (done) => {
// default
assert(vm.$validator1.field1.required === true)
assert(vm.$validator1.field1.required)
assert(vm.$validator1.field1.valid === false)
assert(vm.$validator1.field1.touched === false)
assert(vm.$validator1.field1.dirty === false)
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('checkbox', () => {

it('should be validated', (done) => {
// default
assert(vm.$validator1.field1.required === true)
assert(vm.$validator1.field1.required)
assert(vm.$validator1.field1.minlength === true)
assert(vm.$validator1.field1.valid === false)
assert(vm.$validator1.field1.touched === false)
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('checkbox', () => {
trigger(checkbox2, 'blur')
trigger(checkbox2, 'change')
vm.$nextTick(() => {
assert(vm.$validator1.field1.required === true)
assert(vm.$validator1.field1.required)
assert(vm.$validator1.field1.minlength === true)
assert(vm.$validator1.field1.valid === false)
assert(vm.$validator1.field1.touched === true)
Expand Down Expand Up @@ -201,13 +201,13 @@ describe('checkbox', () => {
trigger(checkbox3, 'change')
trigger(checkbox3, 'blur')
vm.$nextTick(() => {
assert(vm.$validator1.field1.required === true)
assert(vm.$validator1.field1.required)
assert(vm.$validator1.field1.minlength === true)
assert(vm.$validator1.field1.valid === false)
assert(vm.$validator1.field1.touched === true)
assert(vm.$validator1.field1.dirty === true)
assert(vm.$validator1.field1.modified === true)
assert(vm.$validator1.field2.required === true)
assert(vm.$validator1.field2.required)
assert(vm.$validator1.field2.valid === false)
assert(vm.$validator1.field2.touched === true)
assert(vm.$validator1.field2.dirty === true)
Expand Down Expand Up @@ -286,7 +286,7 @@ describe('checkbox', () => {

vm.$set('enabled', false)
vm.$nextTick(() => {
assert(vm.$validator1.field1.required === true)
assert(vm.$validator1.field1.required)
assert(vm.$validator1.field1.valid === false)
assert(vm.$validator1.field1.touched === false)
assert(vm.$validator1.field1.dirty === false)
Expand Down Expand Up @@ -357,7 +357,7 @@ describe('checkbox', () => {
trigger(checkbox2, 'change')
trigger(checkbox2, 'blur')
vm.$nextTick(() => {
assert(vm.$validator1.field1.required === true)
assert(vm.$validator1.field1.required)
assert(vm.$validator1.field1.valid === false)
assert(vm.$validator1.field1.touched === true)
assert(vm.$validator1.field1.dirty === true)
Expand Down
6 changes: 3 additions & 3 deletions test/specs/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('custom', () => {
})

it('should be validated', (done) => {
assert(vm.$validator1.field1.numeric === true)
assert(vm.$validator1.field1)
assert(vm.$validator1.field1.errors[0].validator === 'numeric')
assert(vm.$validator1.field1.errors[0].message === 'invalid numeric value')

Expand Down Expand Up @@ -110,7 +110,7 @@ describe('custom', () => {
})

it('should be validated', (done) => {
assert(vm.$validator1.field1.numeric === true)
assert(vm.$validator1.field1.numeric)
assert(vm.$validator1.field1.errors[0].validator === 'numeric')
assert(vm.$validator1.field1.errors[0].message === (vm.format + 'field1'))

Expand Down Expand Up @@ -154,7 +154,7 @@ describe('custom', () => {
})

it('should be validated', (done) => {
assert(vm.$validator1.field1.required === true)
assert(vm.$validator1.field1.required)
assert(vm.$validator1.field1.errors[0].validator === 'required')
assert(vm.$validator1.field1.errors[0].message === 'required field1')

Expand Down
6 changes: 3 additions & 3 deletions test/specs/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('field', () => {
})

it('should be validated', (done) => {
assert(vm.$validator1.field1.required === true)
assert(vm.$validator1.field1.required)
assert(vm.$validator1.field1.valid === false)
assert(vm.$validator1.field1.dirty === false)
assert(vm.$validator1.field1.modified === false)
Expand Down Expand Up @@ -80,12 +80,12 @@ describe('field', () => {
})

it('should be validated', (done) => {
assert(vm.$validator1.title.minlength === true)
assert(vm.$validator1.title.minlength)
assert(vm.$validator1.title.valid === false)
assert(vm.$validator1.title.dirty === false)
assert(vm.$validator1.title.modified === false)
assert(vm.$validator1.title.touched === false)
assert(vm.$validator1.description.required === true)
assert(vm.$validator1.description.required)
assert(vm.$validator1.description.valid === false)
assert(vm.$validator1.description.dirty === false)
assert(vm.$validator1.description.modified === false)
Expand Down
2 changes: 1 addition & 1 deletion test/specs/radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('radio', () => {
assert(vm.$validator1.field1.touched === false)
assert(vm.$validator1.field1.dirty === false)
assert(vm.$validator1.field1.modified === false)
assert(vm.$validator1.field2.required === true)
assert(vm.$validator1.field2.required)
assert(vm.$validator1.field2.valid === false)
assert(vm.$validator1.field2.touched === false)
assert(vm.$validator1.field2.dirty === false)
Expand Down
8 changes: 4 additions & 4 deletions test/specs/reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('reset', () => {
})

it('should be reset', (done) => {
assert(vm.$validator1.field1.required === true)
assert(vm.$validator1.field1.required)
assert(vm.$validator1.field1.min === false)
assert(vm.$validator1.field1.max === false)
assert(vm.$validator1.field1.valid === false)
Expand Down Expand Up @@ -91,7 +91,7 @@ describe('reset', () => {
})

it('should be reset', (done) => {
assert(vm.$validator1.field1.required === true)
assert(vm.$validator1.field1.required)
assert(vm.$validator1.field1.minlength === true)
assert(vm.$validator1.field1.valid === false)
assert(vm.$validator1.field1.touched === false)
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('reset', () => {
assert(vm.$validator1.field1.touched === false)
assert(vm.$validator1.field1.dirty === false)
assert(vm.$validator1.field1.modified === false)
assert(vm.$validator1.field2.required === true)
assert(vm.$validator1.field2.required)
assert(vm.$validator1.field2.valid === false)
assert(vm.$validator1.field2.touched === false)
assert(vm.$validator1.field2.dirty === false)
Expand Down Expand Up @@ -225,7 +225,7 @@ describe('reset', () => {

it('should be reset', (done) => {
// default
assert(vm.$validator1.lang.required === true)
assert(vm.$validator1.lang.required)
assert(vm.$validator1.lang.minlength === true)
assert(vm.$validator1.lang.valid === false)
assert(vm.$validator1.lang.touched === false)
Expand Down
12 changes: 6 additions & 6 deletions test/specs/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('select', () => {

it('should be validated', (done) => {
// default
assert(vm.$validator1.lang.required === true)
assert(vm.$validator1.lang.required)
assert(vm.$validator1.lang.valid === false)
assert(vm.$validator1.lang.touched === false)
assert(vm.$validator1.lang.dirty === false)
Expand Down Expand Up @@ -190,7 +190,7 @@ describe('select', () => {
trigger(select, 'change')
trigger(select, 'blur')
vm.$nextTick(() => {
assert(vm.$validator1.lang.required === true)
assert(vm.$validator1.lang.required)
assert(vm.$validator1.lang.valid === false)
assert(vm.$validator1.lang.touched === true)
assert(vm.$validator1.lang.dirty === true)
Expand Down Expand Up @@ -231,7 +231,7 @@ describe('select', () => {

it('should be validated', (done) => {
// default
assert(vm.$validator1.lang.required === true)
assert(vm.$validator1.lang.required)
assert(vm.$validator1.lang.minlength === true)
assert(vm.$validator1.lang.valid === false)
assert(vm.$validator1.lang.touched === false)
Expand Down Expand Up @@ -368,7 +368,7 @@ describe('select', () => {

it('should be validated', (done) => {
// default
assert(vm.$validator1.lang.required === true)
assert(vm.$validator1.lang.required)
assert(vm.$validator1.lang.minlength === true)
assert(vm.$validator1.lang.valid === false)
assert(vm.$validator1.lang.touched === false)
Expand All @@ -389,7 +389,7 @@ describe('select', () => {
trigger(select, 'change')
trigger(select, 'blur')
vm.$nextTick(() => {
assert(vm.$validator1.lang.required === true)
assert(vm.$validator1.lang.required)
assert(vm.$validator1.lang.minlength === false)
assert(vm.$validator1.lang.valid === false)
assert(vm.$validator1.lang.touched === true)
Expand All @@ -406,7 +406,7 @@ describe('select', () => {
trigger(select, 'change')
trigger(select, 'blur')
vm.$nextTick(() => {
assert(vm.$validator1.lang.required === true)
assert(vm.$validator1.lang.required)
assert(vm.$validator1.lang.minlength === false)
assert(vm.$validator1.lang.valid === false)
assert(vm.$validator1.lang.touched === true)
Expand Down
8 changes: 4 additions & 4 deletions test/specs/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('$validate', () => {
})

it('should be validated', (done) => {
assert(vm.$validator1.field1.required === true)
assert(vm.$validator1.field1.required)
assert(vm.$validator1.field1.min === false)
assert(vm.$validator1.field1.max === false)
assert(vm.$validator1.field1.valid === false)
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('$validate', () => {
})

it('should be validated', (done) => {
assert(vm.$validator1.field1.required === true)
assert(vm.$validator1.field1.required)
assert(vm.$validator1.field1.minlength === true)
assert(vm.$validator1.field1.valid === false)
assert(vm.$validator1.field1.touched === false)
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('$validate', () => {
assert(vm.$validator1.field1.touched === false)
assert(vm.$validator1.field1.dirty === false)
assert(vm.$validator1.field1.modified === false)
assert(vm.$validator1.field2.required === true)
assert(vm.$validator1.field2.required)
assert(vm.$validator1.field2.valid === false)
assert(vm.$validator1.field2.touched === false)
assert(vm.$validator1.field2.dirty === false)
Expand Down Expand Up @@ -214,7 +214,7 @@ describe('$validate', () => {

it('should be validated', (done) => {
// default
assert(vm.$validator1.lang.required === true)
assert(vm.$validator1.lang.required)
assert(vm.$validator1.lang.minlength === true)
assert(vm.$validator1.lang.valid === false)
assert(vm.$validator1.lang.touched === false)
Expand Down

0 comments on commit d82dd5e

Please sign in to comment.