1
0
mirror of synced 2025-01-19 17:01:43 +03:00

Fixes a race condition that could add validation errors for fields that had been removed

This commit is contained in:
Justin Schroeder 2018-11-01 17:03:05 -04:00
parent 6b19a973a8
commit 55368defd1
3 changed files with 26 additions and 14 deletions

6
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -48,7 +48,8 @@ export default {
return {
parentIdentifier: 'vue-formulate-wrapper-element',
forceErrors: null,
fieldInitials: {}
fieldInitials: {},
whenFinishedValidating: Promise.resolve()
}
},
computed: {
@ -103,13 +104,16 @@ export default {
methods: {
registerField (field, data) {
this.$store.commit(`${this.m}setFieldMeta`, {form: this.name, field, data})
if (data.type !== 'submit') {
this.$store.commit(`${this.m}setFieldValue`, {
field,
value: this.mergedInitial.hasOwnProperty(field) ? this.mergedInitial[field] : undefined,
form: this.name
})
}
},
deregisterField (field) {
async deregisterField (field) {
await this.whenFinishedValidating
this.$store.commit(`${this.m}removeField`, {
form: this.name,
field
@ -151,16 +155,19 @@ export default {
label
}, validation, this.values)
if (!equals(errors || [], (this.validationErrors[field] || []))) {
if (this.fields.find(f => f.name === field)) {
this.updateFieldValidationErrors({field, errors: errors || []})
}
}
return errors
},
updateFormValidation () {
this.fields.map(async field => this.validateField({
async updateFormValidation () {
await this.whenFinishedValidating
this.whenFinishedValidating = Promise.all(this.fields.map(async field => this.validateField({
field: field.name,
validation: field.validation,
label: field.validationLabel || field.label || field.name
}))
})))
},
submit () {
if ((this.prevent === 'validation' && this.hasValidationErrors) || (this.prevent === 'any' && this.hasErrors)) {

View File

@ -90,6 +90,11 @@ export const formulateMutations = (mutations = {}) => Object.assign({
state[group][form] = filter(state[group][form], (key, value) => key !== field)
}
}
},
removeFieldValidationErrors (state, {form, field}) {
state.validationErrors = Object.assign({}, state.validationErrors, {
[form]: filter(state.validationErrors[form] || {}, key => key !== field)
})
}
}, mutations)