my-form elements as array
This commit is contained in:
parent
d65faa41b3
commit
f49153603a
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -1,16 +1,16 @@
|
|||||||
<template>
|
<template>
|
||||||
<form @submit.prevent="submit()">
|
<form @submit.prevent="submit()">
|
||||||
<div v-for="(item, key) in elements" :key="key">
|
<div v-for="(el, idx) in elements" :key="idx">
|
||||||
<component
|
<component
|
||||||
:is="item.element"
|
:is="el.element"
|
||||||
:name="key"
|
:name="el.key"
|
||||||
:label="item.label"
|
:label="el.label"
|
||||||
:description="item.description"
|
:description="el.description"
|
||||||
:props="item.props"
|
:props="el.props"
|
||||||
v-model="formData[key]"
|
v-model="formData[el.key]"
|
||||||
@validate="validateData(key)"
|
@validate="validateData(el.key)"
|
||||||
:invalid="formErrors[key]"
|
:invalid="formErrors[el.key]"
|
||||||
:validatorMessage="formErrors[key] ? formErrors[key].message : ''"
|
:validatorMessage="formErrors[el.key] ? formErrors[el.key].message : ''"
|
||||||
>
|
>
|
||||||
</component>
|
</component>
|
||||||
</div>
|
</div>
|
||||||
@ -24,8 +24,14 @@
|
|||||||
export default {
|
export default {
|
||||||
name: "MyForm",
|
name: "MyForm",
|
||||||
props: {
|
props: {
|
||||||
elements: {
|
initData: {
|
||||||
type: Object,
|
type: Object,
|
||||||
|
default: () => {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
},
|
||||||
|
elements: {
|
||||||
|
type: Array,
|
||||||
default: () => {
|
default: () => {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
@ -45,16 +51,34 @@ export default {
|
|||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
formData: {},
|
formData: this.initData,
|
||||||
formErrors: {}
|
formErrors: {},
|
||||||
|
elementsMap: {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
created() {
|
||||||
|
this.setOrder();
|
||||||
|
},
|
||||||
|
watch: {
|
||||||
|
elements() {
|
||||||
|
this.setOrder();
|
||||||
|
},
|
||||||
|
initData() {
|
||||||
|
this.formData = this.initData;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
setOrder() {
|
||||||
|
this.elementsMap = {};
|
||||||
|
for (let a in this.elements) {
|
||||||
|
this.elementsMap[this.elements[a].key] = this.elements[a];
|
||||||
|
}
|
||||||
|
},
|
||||||
validateData(name) {
|
validateData(name) {
|
||||||
if (this.elements[name].required) {
|
if (this.elementsMap[name].required) {
|
||||||
if (!this.formData[name]) {
|
if (!this.formData[name]) {
|
||||||
this.$set(this.formErrors, name, {
|
this.$set(this.formErrors, name, {
|
||||||
message: this.elements[name].requiredMessage
|
message: this.elementsMap[name].requiredMessage
|
||||||
});
|
});
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -73,7 +97,7 @@ export default {
|
|||||||
},
|
},
|
||||||
submit() {
|
submit() {
|
||||||
let valid = true;
|
let valid = true;
|
||||||
Object.keys(this.elements).forEach(key => {
|
Object.keys(this.elementsMap).forEach(key => {
|
||||||
valid = (this.validateData(key) && valid);
|
valid = (this.validateData(key) && valid);
|
||||||
});
|
});
|
||||||
if (valid) {
|
if (valid) {
|
||||||
|
@ -23,8 +23,9 @@ export default {
|
|||||||
return {
|
return {
|
||||||
username: "",
|
username: "",
|
||||||
password: "",
|
password: "",
|
||||||
elements: {
|
elements: [
|
||||||
username: {
|
{
|
||||||
|
key: "username",
|
||||||
element: "my-input",
|
element: "my-input",
|
||||||
icon: "icon-user",
|
icon: "icon-user",
|
||||||
required: true,
|
required: true,
|
||||||
@ -34,7 +35,8 @@ export default {
|
|||||||
placeholder: "Benutzername"
|
placeholder: "Benutzername"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
password: {
|
{
|
||||||
|
key: "password",
|
||||||
element: "my-input",
|
element: "my-input",
|
||||||
icon: "icon-key",
|
icon: "icon-key",
|
||||||
required: true,
|
required: true,
|
||||||
@ -44,7 +46,7 @@ export default {
|
|||||||
placeholder: "Passwort"
|
placeholder: "Passwort"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
],
|
||||||
buttons: [{
|
buttons: [{
|
||||||
label: "login",
|
label: "login",
|
||||||
type: "submit"
|
type: "submit"
|
||||||
|
Loading…
Reference in New Issue
Block a user