1
0
mirror of synced 2024-12-01 17:46:07 +03:00
vue-formulario/src~v1/components/Formulate.vue

182 lines
4.8 KiB
Vue
Raw Normal View History

2018-01-31 01:21:21 +03:00
<template>
<form
@submit.prevent="submit"
class="formulate-form"
2018-01-31 01:21:21 +03:00
>
<slot />
</form>
</template>
<script>
import {equals, reduce} from '../utils'
import cloneDeep from 'clone-deep'
2018-01-31 01:21:21 +03:00
export default {
props: {
name: {
type: String,
required: true
},
module: {
type: [String, Boolean],
default: function () {
return this.$formulate.options.vuexModule
}
},
initial: {
type: Object,
default: () => ({})
2018-02-01 01:20:29 +03:00
},
behavior: {
type: String,
default: 'blur'
},
showErrors: {
type: [Boolean, Object],
default: () => ({})
},
errors: {
type: Object,
default: () => ({})
},
prevent: {
type: String,
default: 'validation'
2018-01-31 01:21:21 +03:00
}
},
data () {
return {
2018-02-01 01:20:29 +03:00
parentIdentifier: 'vue-formulate-wrapper-element',
forceErrors: null,
fieldInitials: {},
whenFinishedValidating: Promise.resolve()
2018-01-31 01:21:21 +03:00
}
},
computed: {
m () {
return `${this.module ? this.module + '/' : ''}`
},
hasErrors () {
return this.$store.getters[`${this.m}hasErrors`][this.name] || false
},
hasValidationErrors () {
return this.$store.getters[`${this.m}hasValidationErrors`][this.name] || false
},
2018-01-31 01:21:21 +03:00
values () {
return cloneDeep(this.$store.getters[`${this.m}formValues`][this.name] || {})
2018-01-31 01:21:21 +03:00
},
storeErrors () {
2018-01-31 01:21:21 +03:00
return this.$store.getters[`${this.m}formErrors`][this.name] || {}
},
validationErrors () {
return this.$store.getters[`${this.m}formValidationErrors`][this.name] || {}
},
fields () {
return this.$store.getters[`${this.m}formMeta`][this.name] || []
2018-02-01 01:20:29 +03:00
},
shouldShowErrors () {
if (this.forceErrors === false || this.forceErrors === true) {
return this.forceErrors
}
if (this.showErrors === false || this.showErrors === true) {
return this.showErrors
}
return this.behavior === 'live'
},
mergedInitial () {
return Object.assign({}, this.initial, this.fieldInitials)
2018-01-31 01:21:21 +03:00
}
},
watch: {
mergedInitial () {
this.hydrate(this.mergedInitial)
},
values () {
this.updateFormValidation()
}
},
2018-01-31 01:21:21 +03:00
created () {
this.hydrate(this.mergedInitial)
2018-01-31 01:21:21 +03:00
},
2018-02-01 21:15:41 +03:00
mounted () {
this.hydrate(this.mergedInitial)
2018-02-01 21:15:41 +03:00
},
2018-01-31 01:21:21 +03:00
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
})
}
},
async deregisterField (field) {
await this.whenFinishedValidating
this.$store.commit(`${this.m}removeField`, {
form: this.name,
field
})
},
2018-01-31 01:21:21 +03:00
hydrate (values) {
for (let field of this.fields) {
if (field.type !== 'submit' && typeof this.values[field.name] === 'undefined') {
2018-02-01 20:23:39 +03:00
this.$store.commit(`${this.m}setFieldValue`, {
field: field.name,
value: values[field.name],
form: this.name
})
}
2018-01-31 01:21:21 +03:00
}
},
setInitial (field, value) {
this.fieldInitials = Object.assign({}, this.fieldInitials, {[field]: value})
},
2018-01-31 01:21:21 +03:00
update (change) {
this.$store.commit(`${this.m}setFieldValue`, Object.assign(change, {
form: this.name
}))
},
updateFieldErrors (change) {
this.$store.commit(`${this.m}setFieldErrors`, Object.assign(change, {
form: this.name
}))
},
updateFieldValidationErrors (change) {
this.$store.commit(`${this.m}setFieldValidationErrors`, Object.assign(change, {
form: this.name
}))
},
async validateField ({field, validation, label}) {
2018-01-31 01:21:21 +03:00
let errors = await this.$formulate.validationErrors({
field,
value: this.values[field],
label
2018-01-31 01:21:21 +03:00
}, validation, this.values)
2018-02-01 01:20:29 +03:00
if (!equals(errors || [], (this.validationErrors[field] || []))) {
if (this.fields.find(f => f.name === field)) {
this.updateFieldValidationErrors({field, errors: errors || []})
}
2018-01-31 01:21:21 +03:00
}
return errors
},
async updateFormValidation () {
await this.whenFinishedValidating
this.whenFinishedValidating = Promise.all(this.fields.map(async field => this.validateField({
2018-01-31 01:21:21 +03:00
field: field.name,
validation: field.validation,
label: field.validationLabel || field.label || field.name
})))
2018-01-31 01:21:21 +03:00
},
submit () {
if ((this.prevent === 'validation' && this.hasValidationErrors) || (this.prevent === 'any' && this.hasErrors)) {
2018-02-01 01:20:29 +03:00
this.forceErrors = true
} else {
this.$emit('submit', Object.assign({}, this.values))
2018-02-01 01:20:29 +03:00
}
2018-01-31 01:21:21 +03:00
}
}
2018-01-31 01:21:21 +03:00
}
</script>