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

Commit

Permalink
feat(api): fix $validate API when using touched (#211) by @losadaem
Browse files Browse the repository at this point in the history
Add touched option to `willUpdateFlags` in `RadioValidation` and
`CheckboxValidation` classes.

In 4635ce4 $validate API was updated with touched option. But this
was not implemented in `BaseValidation` subclasses.

Add tests for all validatable elements

Fixes: #210
  • Loading branch information
losadaem authored and kazupon committed Apr 28, 2016
1 parent 9f46ebf commit 68282d1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/validations/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ export default class CheckboxValidation extends BaseValidation {
this._validator.validate({ field: this.field })
}

willUpdateFlags () {
willUpdateFlags (touched = false) {
each(this._inits, (item, index) => {
touched && this.willUpdateTouched(item.el, 'blur')
this.willUpdateDirty(item.el)
this.willUpdateModified(item.el)
})
Expand Down
3 changes: 2 additions & 1 deletion src/validations/radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ export default class RadioValidation extends BaseValidation {
this._validator.validate({ field: this.field })
}

willUpdateFlags () {
willUpdateFlags (touched = false) {
each(this._inits, (item, index) => {
touched && this.willUpdateTouched(item.el, 'blur')
this.willUpdateDirty(item.el)
this.willUpdateModified(item.el)
})
Expand Down
57 changes: 56 additions & 1 deletion test/specs/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('$validate', () => {
assert(vm.$validator1.dirty === true)
assert(vm.$validator1.modified === true)
assert(vm.$validator1.touched === false)

done()
})
})
Expand Down Expand Up @@ -254,6 +254,22 @@ describe('$validate', () => {
+ '<form novalidate>'
+ '<input type="number" v-validate:field1="{ required: true, min: 0, max: 10 }">'
+ '<input type="text" value="hello" v-validate:field2="{ minlength: 4 }">'
+ '<input type="checkbox" value="foo" v-validate:checkbox="{ required: true, minlength: 1 }">'
+ '<input type="checkbox" value="bar" v-validate:checkbox>'
+ '<input type="checkbox" value="buz" v-validate:checkbox>'
+ '<fieldset>'
+ '<label for="radio1">radio1</label>'
+ '<input type="radio" id="radio1" name="r1" checked value="foo" v-validate:radio="{ required: true }">'
+ '<label for="radio2">radio2</label>'
+ '<input type="radio" id="radio2" name="r1" value="bar" v-validate:radio="{ required: true }">'
+ '</fieldset>'
+ '<select multiple v-validate:select="{ required: true, minlength: 2 }">'
+ '<option value="en">english</option>'
+ '<option value="ja">japanese</option>'
+ '<option value="zh">chinese</option>'
+ '<option value="fr">french</option>'
+ '<option value="de">German</option>'
+ '</select>'
+ '</form>'
+ '</validator>'
vm = new Vue({ el: el })
Expand Down Expand Up @@ -353,5 +369,44 @@ describe('$validate', () => {
})
})
})

describe('touched for all validatable elements', () => {
it('should be validated', (done) => {
assert(vm.$validator1.field1.touched === false)
assert(vm.$validator1.field2.touched === false)
assert(vm.$validator1.checkbox.touched === false)
assert(vm.$validator1.radio.touched === false)
assert(vm.$validator1.select.touched === false)
assert(vm.$validator1.touched === false)

vm.$nextTick(() => {
vm.$validate('field2')
vm.$validate('checkbox')
vm.$validate('radio')
vm.$validate('select')

assert(vm.$validator1.field1.touched === false)
assert(vm.$validator1.field2.touched === false)
assert(vm.$validator1.checkbox.touched === false)
assert(vm.$validator1.radio.touched === false)
assert(vm.$validator1.select.touched === false)
assert(vm.$validator1.touched === false)

vm.$validate('field2', true)
vm.$validate('checkbox', true)
vm.$validate('radio', true)
vm.$validate('select', true)

assert(vm.$validator1.field1.touched === false)
assert(vm.$validator1.field2.touched === true)
assert(vm.$validator1.checkbox.touched === true)
assert(vm.$validator1.radio.touched === true)
assert(vm.$validator1.select.touched === true)
assert(vm.$validator1.touched === true)

done()
})
})
})
})
})

0 comments on commit 68282d1

Please sign in to comment.