Skip to content

Commit ba64199

Browse files
committed
fix(form): item validator validate sync
1 parent 69bcf99 commit ba64199

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

src/components/form/form-item.vue

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
v-model="originValid"
1212
:disabled="validatorDisabled"
1313
:model="validatorModel"
14+
:model-key="validatorModelKey"
1415
:rules="fieldValue.rules"
1516
:messages="fieldValue.messages"
1617
@input="validatorChangeHandler"
@@ -59,12 +60,16 @@
5960
}
6061
},
6162
data() {
63+
const validatorModelKey = 'value'
6264
const modelKey = this.field.modelKey
6365
const modelValue = modelKey ? this.form.model[modelKey] : null
6466
return {
6567
validatorDisabled: false,
68+
validatorModelKey,
6669
modelValue: modelValue,
67-
validatorModel: modelValue
70+
validatorModel: {
71+
[validatorModelKey]: modelValue
72+
}
6873
}
6974
},
7075
computed: {
@@ -112,10 +117,13 @@
112117
this.modelValue = newModel
113118
}
114119
},
115-
modelValue(newModel) {
116-
// update form model
117-
this.form.model[this.fieldValue.modelKey] = newModel
118-
this.updateValidatorModel()
120+
modelValue: {
121+
handler(newModel) {
122+
// update form model
123+
this.form.model[this.fieldValue.modelKey] = newModel
124+
this.updateValidatorModel()
125+
},
126+
sync: true
119127
},
120128
originValid(newVal) {
121129
this.lastOriginValid = newVal
@@ -144,30 +152,19 @@
144152
if ((!debounceTime && debounceTime !== 0) || debounceTime < 0 || this.fieldValue.trigger === 'blur') return
145153
this.getValidatorModel = debounce((modelValue) => {
146154
this.pending = false
147-
this.validatorModel = modelValue
155+
this.validatorModel[this.validatorModelKey] = modelValue
148156
this.form.updatePending()
149-
this.asyncSameCheck()
157+
this.validate()
150158
return modelValue
151-
}, debounceTime, false, this.validatorModel)
152-
},
153-
asyncSameCheck() {
154-
const validator = this.$refs.validator
155-
const validatorModel = this.validatorModel
156-
if (validator) {
157-
// same value, Vue do not trigger watch handler
158-
// so need to force validate
159-
if (validatorModel === validator.model) {
160-
validator && validator.validate()
161-
}
162-
}
159+
}, debounceTime, false, this.validatorModel[this.validatorModelKey])
163160
},
164161
focusInHandler() {
165162
this.focused = true
166163
},
167164
focusOutHandler() {
168165
this.focused = false
169166
this.updateValidatorModel()
170-
this.asyncSameCheck()
167+
this.validate()
171168
},
172169
initFocusEvents() {
173170
if (this.fieldValue.trigger === 'blur') {
@@ -176,7 +173,7 @@
176173
formItem.addEventListener(EVENT_FOCUSOUT, this.focusOutHandler, false)
177174
this.getValidatorModel = (modelValue) => {
178175
if (this.focused) {
179-
return this.validatorModel
176+
return this.validatorModel[this.validatorModelKey]
180177
} else {
181178
this.pending = false
182179
this.form.updatePending()
@@ -192,7 +189,7 @@
192189
},
193190
updateValidatorModel() {
194191
this.pending = true
195-
this.validatorModel = this.getValidatorModel(this.modelValue)
192+
this.validatorModel[this.validatorModelKey] = this.getValidatorModel(this.modelValue)
196193
if (this.pending) {
197194
this.form.setPending(this.pending)
198195
this.originValid = undefined

0 commit comments

Comments
 (0)