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