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

Commit

Permalink
feat(validations): support validation result resetting
Browse files Browse the repository at this point in the history
Closes #136
  • Loading branch information
kazupon committed Jan 24, 2016
1 parent 924837f commit a66909f
Show file tree
Hide file tree
Showing 7 changed files with 393 additions and 77 deletions.
37 changes: 23 additions & 14 deletions src/validations/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,9 @@ export default class BaseValidation {
this._el = el
this._forScope = scope
this._init = this._getValue(el)
this._value = el.value
this._validators = {}
}

_getValue (el) {
return el.value
}

_getScope () {
return this._forScope || this._vm
}

manageElement (el) {
const _ = util.Vue.util

Expand All @@ -39,7 +30,6 @@ export default class BaseValidation {
if (model) {
el.value = scope.$get(model) || ''
this._unwatch = scope.$watch(model, _.bind((val, old) => {
console.log('BaseValidation#manageElement $watch', model, val, old)
if (val !== old) {
this.handleValidate(el)
}
Expand Down Expand Up @@ -89,10 +79,6 @@ export default class BaseValidation {
this._validator.validate()
}

_checkModified (target) {
return this._init !== this._getValue(target)
}

validate () {
const _ = util.Vue.util

Expand Down Expand Up @@ -153,6 +139,29 @@ export default class BaseValidation {
return results
}

resetFlags () {
this.touched = false
this.dirty = false
this.modified = false
}

reset () {
this.resetFlags()
this._init = this._getValue(this._el)
}

_getValue (el) {
return el.value
}

_getScope () {
return this._forScope || this._vm
}

_checkModified (target) {
return this._init !== this._getValue(target)
}

_fireEvent (el, valid) {
trigger(el, valid ? 'valid' : 'invalid')
}
Expand Down
46 changes: 27 additions & 19 deletions src/validations/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,6 @@ export default class CheckboxValidation extends BaseValidation {
this._inits = []
}

_addItem (el) {
let item = {
el: el,
init: el.checked,
value: el.value
}
this._inits.push(item)
return item
}

_setChecked (values, el) {
for (let i = 0, l = values.length; i < l; i++) {
let value = values[i]
if (!el.disabled && el.value === value && !el.checked) {
el.checked = true
}
}
}

manageElement (el) {
const _ = util.Vue.util

Expand Down Expand Up @@ -82,6 +63,33 @@ export default class CheckboxValidation extends BaseValidation {
this._validator.validate()
}

reset () {
this.resetFlags()
each(this._inits, (item, index) => {
item.init = item.el.checked
item.value = item.el.value
})
}

_addItem (el) {
let item = {
el: el,
init: el.checked,
value: el.value
}
this._inits.push(item)
return item
}

_setChecked (values, el) {
for (let i = 0, l = values.length; i < l; i++) {
let value = values[i]
if (!el.disabled && el.value === value && !el.checked) {
el.checked = true
}
}
}

_getValue (el) {
if (!this._inits || this._inits.length === 0) {
return el.checked
Expand Down
46 changes: 27 additions & 19 deletions src/validations/radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,6 @@ export default class RadioValidation extends BaseValidation {
this._inits = []
}

_addItem (el) {
let item = {
el: el,
init: el.checked,
value: el.value
}
this._inits.push(item)
return item
}

_setChecked (value, el, item) {
if (el.value === value) {
el.checked = true
this._init = el.checked
item.init = el.checked
item.value = value
}
}

manageElement (el) {
const _ = util.Vue.util

Expand Down Expand Up @@ -65,6 +46,33 @@ export default class RadioValidation extends BaseValidation {
this._validator.validate()
}

reset () {
this.resetFlags()
each(this._inits, (item, index) => {
item.init = item.el.checked
item.value = item.el.value
})
}

_addItem (el) {
let item = {
el: el,
init: el.checked,
value: el.value
}
this._inits.push(item)
return item
}

_setChecked (value, el, item) {
if (el.value === value) {
el.checked = true
this._init = el.checked
item.init = el.checked
item.value = value
}
}

_getValue (el) {
if (!this._inits || this._inits.length === 0) {
return el.checked
Expand Down
54 changes: 29 additions & 25 deletions src/validations/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@ export default class SelectValidation extends BaseValidation {
this._multiple = this._el.hasAttribute('multiple')
}

manageElement (el) {
const _ = util.Vue.util

let scope = this._getScope()
let model = this._model
if (model) {
let value = scope.$get(model)
let values = !Array.isArray(value) ? [value] : value
this._setOption(values, el)
this._unwatch = scope.$watch(model, _.bind((val, old) => {
let values1 = !Array.isArray(val) ? [val] : val
let values2 = !Array.isArray(old) ? [old] : old
if (values1.slice().sort().toString() !== values2.slice().sort().toString()) {
this.handleValidate(el)
}
}, this))
}
}

unmanageElement (el) {
if (this._unwatch) {
this._unwatch()
}
}

reset () {
this.resetFlags()
}

_getValue (el) {
let ret = []

Expand All @@ -40,31 +69,6 @@ export default class SelectValidation extends BaseValidation {
}
}

manageElement (el) {
const _ = util.Vue.util

let scope = this._getScope()
let model = this._model
if (model) {
let value = scope.$get(model)
let values = !Array.isArray(value) ? [value] : value
this._setOption(values, el)
this._unwatch = scope.$watch(model, _.bind((val, old) => {
let values1 = !Array.isArray(val) ? [val] : val
let values2 = !Array.isArray(old) ? [old] : old
if (values1.slice().sort().toString() !== values2.slice().sort().toString()) {
this.handleValidate(el)
}
}, this))
}
}

unmanageElement (el) {
if (this._unwatch) {
this._unwatch()
}
}

_checkModified (target) {
let values = this._getValue(target).slice().sort()
if (this._init.length !== values.length) {
Expand Down
21 changes: 21 additions & 0 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,34 @@ export default class Validator {
enableReactive () {
util.Vue.util.defineReactive(this._dir.vm, this.name, this._scope)
this._dir.vm._validatorMaps[this.name] = this

this._dir.vm.$validatorReset = util.Vue.util.bind(() => {
this.resetValidation()
}, this)
}

disableReactive () {
this._dir.vm.$validatorReset = null
this._dir.vm._validatorMaps[this.name] = null
this._dir.vm[this.name] = null
}

resetValidation () {
each(this._validations, (validation, key) => {
validation.reset()
}, this)

each(this._checkboxValidations, (dataset, key) => {
dataset.validation.reset()
}, this)

each(this._radioValidations, (dataset, key) => {
dataset.validation.reset()
}, this)

this.validate()
}

// TODO: should be improved performance (use cache)
get validations () {
const extend = util.Vue.util.extend
Expand Down
1 change: 1 addition & 0 deletions test/specs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ require('./lazy')
require('./checkbox')
require('./radio')
require('./select')
require('./reset')
Loading

0 comments on commit a66909f

Please sign in to comment.