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

Commit 68282d1

Browse files
losadaemkazupon
authored andcommitted
feat(api): fix $validate API when using touched (#211) by @losadaem
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
1 parent 9f46ebf commit 68282d1

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

src/validations/checkbox.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ export default class CheckboxValidation extends BaseValidation {
7373
this._validator.validate({ field: this.field })
7474
}
7575

76-
willUpdateFlags () {
76+
willUpdateFlags (touched = false) {
7777
each(this._inits, (item, index) => {
78+
touched && this.willUpdateTouched(item.el, 'blur')
7879
this.willUpdateDirty(item.el)
7980
this.willUpdateModified(item.el)
8081
})

src/validations/radio.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ export default class RadioValidation extends BaseValidation {
5353
this._validator.validate({ field: this.field })
5454
}
5555

56-
willUpdateFlags () {
56+
willUpdateFlags (touched = false) {
5757
each(this._inits, (item, index) => {
58+
touched && this.willUpdateTouched(item.el, 'blur')
5859
this.willUpdateDirty(item.el)
5960
this.willUpdateModified(item.el)
6061
})

test/specs/validate.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('$validate', () => {
6363
assert(vm.$validator1.dirty === true)
6464
assert(vm.$validator1.modified === true)
6565
assert(vm.$validator1.touched === false)
66-
66+
6767
done()
6868
})
6969
})
@@ -254,6 +254,22 @@ describe('$validate', () => {
254254
+ '<form novalidate>'
255255
+ '<input type="number" v-validate:field1="{ required: true, min: 0, max: 10 }">'
256256
+ '<input type="text" value="hello" v-validate:field2="{ minlength: 4 }">'
257+
+ '<input type="checkbox" value="foo" v-validate:checkbox="{ required: true, minlength: 1 }">'
258+
+ '<input type="checkbox" value="bar" v-validate:checkbox>'
259+
+ '<input type="checkbox" value="buz" v-validate:checkbox>'
260+
+ '<fieldset>'
261+
+ '<label for="radio1">radio1</label>'
262+
+ '<input type="radio" id="radio1" name="r1" checked value="foo" v-validate:radio="{ required: true }">'
263+
+ '<label for="radio2">radio2</label>'
264+
+ '<input type="radio" id="radio2" name="r1" value="bar" v-validate:radio="{ required: true }">'
265+
+ '</fieldset>'
266+
+ '<select multiple v-validate:select="{ required: true, minlength: 2 }">'
267+
+ '<option value="en">english</option>'
268+
+ '<option value="ja">japanese</option>'
269+
+ '<option value="zh">chinese</option>'
270+
+ '<option value="fr">french</option>'
271+
+ '<option value="de">German</option>'
272+
+ '</select>'
257273
+ '</form>'
258274
+ '</validator>'
259275
vm = new Vue({ el: el })
@@ -353,5 +369,44 @@ describe('$validate', () => {
353369
})
354370
})
355371
})
372+
373+
describe('touched for all validatable elements', () => {
374+
it('should be validated', (done) => {
375+
assert(vm.$validator1.field1.touched === false)
376+
assert(vm.$validator1.field2.touched === false)
377+
assert(vm.$validator1.checkbox.touched === false)
378+
assert(vm.$validator1.radio.touched === false)
379+
assert(vm.$validator1.select.touched === false)
380+
assert(vm.$validator1.touched === false)
381+
382+
vm.$nextTick(() => {
383+
vm.$validate('field2')
384+
vm.$validate('checkbox')
385+
vm.$validate('radio')
386+
vm.$validate('select')
387+
388+
assert(vm.$validator1.field1.touched === false)
389+
assert(vm.$validator1.field2.touched === false)
390+
assert(vm.$validator1.checkbox.touched === false)
391+
assert(vm.$validator1.radio.touched === false)
392+
assert(vm.$validator1.select.touched === false)
393+
assert(vm.$validator1.touched === false)
394+
395+
vm.$validate('field2', true)
396+
vm.$validate('checkbox', true)
397+
vm.$validate('radio', true)
398+
vm.$validate('select', true)
399+
400+
assert(vm.$validator1.field1.touched === false)
401+
assert(vm.$validator1.field2.touched === true)
402+
assert(vm.$validator1.checkbox.touched === true)
403+
assert(vm.$validator1.radio.touched === true)
404+
assert(vm.$validator1.select.touched === true)
405+
assert(vm.$validator1.touched === true)
406+
407+
done()
408+
})
409+
})
410+
})
356411
})
357412
})

0 commit comments

Comments
 (0)