1
0
mirror of synced 2024-11-25 23:06:02 +03:00

Adds support for new errors prop on the Formulate element that passes errors down to child elements. Also forms no longer prevent submission if they only have local errors. This can be changed with a prevent="all" prop on the Formulate element.

This commit is contained in:
Justin Schroeder 2018-02-15 23:21:29 -05:00
parent d4443a1e2c
commit 46ac900f0e
4 changed files with 32 additions and 13 deletions

16
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,7 +1,7 @@
<template> <template>
<form <form
@submit.prevent="submit" @submit.prevent="submit"
class="formulate-element" class="formulate-form"
> >
<slot /> <slot />
</form> </form>
@ -34,6 +34,14 @@ export default {
showErrors: { showErrors: {
type: [Boolean, Object], type: [Boolean, Object],
default: () => ({}) default: () => ({})
},
errors: {
type: Object,
default: () => ({})
},
prevent: {
type: String,
default: 'validation'
} }
}, },
data () { data () {
@ -49,10 +57,13 @@ export default {
hasErrors () { hasErrors () {
return this.$store.getters[`${this.m}hasErrors`][this.name] || false return this.$store.getters[`${this.m}hasErrors`][this.name] || false
}, },
hasValidationErrors () {
return this.$store.getters[`${this.m}hasValidationErrors`][this.name] || false
},
values () { values () {
return cloneDeep(this.$store.getters[`${this.m}formValues`][this.name] || {}) return cloneDeep(this.$store.getters[`${this.m}formValues`][this.name] || {})
}, },
errors () { storeErrors () {
return this.$store.getters[`${this.m}formErrors`][this.name] || {} return this.$store.getters[`${this.m}formErrors`][this.name] || {}
}, },
validationErrors () { validationErrors () {
@ -134,12 +145,12 @@ export default {
})) }))
}, },
submit () { submit () {
if (this.hasErrors) { if ((this.prevent === 'validation' && this.hasValidationErrors) || (this.prevent === 'any' && this.hasErrors)) {
this.forceErrors = true this.forceErrors = true
} else { } else {
this.$emit('submit', Object.assign({}, this.values)) this.$emit('submit', Object.assign({}, this.values))
} }
} }
}, }
} }
</script> </script>

View File

@ -229,10 +229,13 @@ export default {
return this.form.validationErrors[this.name] || [] return this.form.validationErrors[this.name] || []
}, },
storeErrors () { storeErrors () {
return this.form.storeErrors[this.name] || []
},
formErrors () {
return this.form.errors[this.name] || [] return this.form.errors[this.name] || []
}, },
localAndValidationErrors () { localAndValidationErrors () {
return this.errors.concat(this.validationErrors) return this.errors.concat(this.validationErrors).concat(this.formErrors)
}, },
shouldShowErrors () { shouldShowErrors () {
let show = this.form.shouldShowErrors let show = this.form.shouldShowErrors

View File

@ -36,6 +36,11 @@ export const formulateGetters = (moduleName = '', getters = {}) => Object.assign
return map(state.errors, (form, errors) => { return map(state.errors, (form, errors) => {
return reduce(errors, (hasErrors, field, errors) => hasErrors || !!errors.length, false) return reduce(errors, (hasErrors, field, errors) => hasErrors || !!errors.length, false)
}) })
},
hasValidationErrors (state) {
return map(state.validationErrors, (form, errors) => {
return reduce(errors, (hasErrors, field, errors) => hasErrors || !!errors.length, false)
})
} }
}, getters) }, getters)