2018-01-31 01:21:21 +03:00
|
|
|
<template>
|
|
|
|
<form
|
|
|
|
@submit.prevent="submit"
|
2018-02-16 07:21:29 +03:00
|
|
|
class="formulate-form"
|
2018-01-31 01:21:21 +03:00
|
|
|
>
|
|
|
|
<slot />
|
|
|
|
</form>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2018-02-16 22:29:41 +03:00
|
|
|
import {equals, reduce} from '../utils'
|
2018-02-02 20:07:51 +03:00
|
|
|
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: () => ({})
|
2018-02-16 07:21:29 +03:00
|
|
|
},
|
|
|
|
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',
|
2018-02-16 07:55:36 +03:00
|
|
|
forceErrors: null,
|
2018-11-02 00:03:05 +03:00
|
|
|
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
|
|
|
|
},
|
2018-02-16 07:21:29 +03:00
|
|
|
hasValidationErrors () {
|
|
|
|
return this.$store.getters[`${this.m}hasValidationErrors`][this.name] || false
|
|
|
|
},
|
2018-01-31 01:21:21 +03:00
|
|
|
values () {
|
2018-02-02 20:07:51 +03:00
|
|
|
return cloneDeep(this.$store.getters[`${this.m}formValues`][this.name] || {})
|
2018-01-31 01:21:21 +03:00
|
|
|
},
|
2018-02-16 07:21:29 +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 () {
|
2018-02-01 20:14:19 +03:00
|
|
|
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'
|
2018-02-16 07:55:36 +03:00
|
|
|
},
|
|
|
|
mergedInitial () {
|
2018-02-16 08:19:11 +03:00
|
|
|
return Object.assign({}, this.initial, this.fieldInitials)
|
2018-01-31 01:21:21 +03:00
|
|
|
}
|
|
|
|
},
|
2018-02-05 22:17:26 +03:00
|
|
|
watch: {
|
2018-02-16 07:55:36 +03:00
|
|
|
mergedInitial () {
|
|
|
|
this.hydrate(this.mergedInitial)
|
2018-04-10 17:51:26 +03:00
|
|
|
},
|
|
|
|
values () {
|
|
|
|
this.updateFormValidation()
|
2018-02-05 22:17:26 +03:00
|
|
|
}
|
|
|
|
},
|
2018-01-31 01:21:21 +03:00
|
|
|
created () {
|
2018-02-16 07:55:36 +03:00
|
|
|
this.hydrate(this.mergedInitial)
|
2018-01-31 01:21:21 +03:00
|
|
|
},
|
2018-02-01 21:15:41 +03:00
|
|
|
mounted () {
|
2018-02-16 07:55:36 +03:00
|
|
|
this.hydrate(this.mergedInitial)
|
2018-02-01 21:15:41 +03:00
|
|
|
},
|
2018-01-31 01:21:21 +03:00
|
|
|
methods: {
|
2018-02-01 20:14:19 +03:00
|
|
|
registerField (field, data) {
|
|
|
|
this.$store.commit(`${this.m}setFieldMeta`, {form: this.name, field, data})
|
2018-11-02 00:03:05 +03:00
|
|
|
if (data.type !== 'submit') {
|
|
|
|
this.$store.commit(`${this.m}setFieldValue`, {
|
|
|
|
field,
|
|
|
|
value: this.mergedInitial.hasOwnProperty(field) ? this.mergedInitial[field] : undefined,
|
|
|
|
form: this.name
|
|
|
|
})
|
|
|
|
}
|
2018-02-01 20:14:19 +03:00
|
|
|
},
|
2018-11-02 00:03:05 +03:00
|
|
|
async deregisterField (field) {
|
|
|
|
await this.whenFinishedValidating
|
2018-03-07 23:44:35 +03:00
|
|
|
this.$store.commit(`${this.m}removeField`, {
|
|
|
|
form: this.name,
|
|
|
|
field
|
|
|
|
})
|
|
|
|
},
|
2018-01-31 01:21:21 +03:00
|
|
|
hydrate (values) {
|
2018-02-05 22:17:26 +03:00
|
|
|
for (let field of this.fields) {
|
2018-02-28 19:47:34 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
},
|
2018-02-16 07:55:36 +03:00
|
|
|
setInitial (field, value) {
|
2018-02-16 22:29:41 +03:00
|
|
|
this.fieldInitials = Object.assign({}, this.fieldInitials, {[field]: value})
|
2018-02-16 07:55:36 +03:00
|
|
|
},
|
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
|
|
|
|
}))
|
|
|
|
},
|
2018-02-01 18:47:11 +03:00
|
|
|
async validateField ({field, validation, label}) {
|
2018-01-31 01:21:21 +03:00
|
|
|
let errors = await this.$formulate.validationErrors({
|
|
|
|
field,
|
2018-02-01 18:47:11 +03:00
|
|
|
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] || []))) {
|
2018-11-02 00:03:05 +03:00
|
|
|
if (this.fields.find(f => f.name === field)) {
|
|
|
|
this.updateFieldValidationErrors({field, errors: errors || []})
|
|
|
|
}
|
2018-01-31 01:21:21 +03:00
|
|
|
}
|
|
|
|
return errors
|
|
|
|
},
|
2018-11-02 00:03:05 +03:00
|
|
|
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,
|
2018-02-01 18:47:11 +03:00
|
|
|
validation: field.validation,
|
|
|
|
label: field.validationLabel || field.label || field.name
|
2018-11-02 00:03:05 +03:00
|
|
|
})))
|
2018-01-31 01:21:21 +03:00
|
|
|
},
|
|
|
|
submit () {
|
2018-02-16 07:21:29 +03:00
|
|
|
if ((this.prevent === 'validation' && this.hasValidationErrors) || (this.prevent === 'any' && this.hasErrors)) {
|
2018-02-01 01:20:29 +03:00
|
|
|
this.forceErrors = true
|
|
|
|
} else {
|
2018-02-01 19:02:58 +03:00
|
|
|
this.$emit('submit', Object.assign({}, this.values))
|
2018-02-01 01:20:29 +03:00
|
|
|
}
|
2018-01-31 01:21:21 +03:00
|
|
|
}
|
2018-02-16 07:21:29 +03:00
|
|
|
}
|
2018-01-31 01:21:21 +03:00
|
|
|
}
|
|
|
|
</script>
|