-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmForm.vue
134 lines (130 loc) · 2.36 KB
/
EmForm.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<template>
<form :class="inline && inlineClasses" @keyup.escape="$emit('esc')">
<!-- @submit.prevent="" -->
<slot :v="$v" :sent="sent" :sentChange="sentChange" :form="form"></slot>
</form>
</template>
<script>
import {
required,
requiredIf,
requiredUnless,
minLength,
maxLength,
minValue,
maxValue,
alpha,
alphaNum,
numeric,
decimal,
integer,
email,
ipAddress,
between,
url,
helpers,
} from 'vuelidate/lib/validators';
export default {
name: 'EmForm',
props: {
validationProps: {
type: Array,
required: true,
validator: prop => prop.every(e => typeof e === 'object'),
},
inline: { type: Boolean, default: false },
},
components: {
//
},
data() {
return {
sent: false,
form: {},
};
},
validations() {
let schema = {
form: {
//
// property: {
// required,
// url,
// minLength: minLength(3),
// },
},
};
//populate schema.form with properties
this.validationProps.forEach(valObj => {
const rules = valObj.rules;
rules.forEach(rule => {
if (typeof rule !== 'object') {
schema.form[valObj.name] = {
...schema.form[valObj.name],
[rule]: this[rule],
};
} else {
schema.form[valObj.name] = {
...schema.form[valObj.name],
[rule.name]: this[rule.name](...rule.params),
};
}
});
});
return schema;
},
created() {
this.validationProps.forEach(valObj => {
//set must be used since we are dynamically adding properties
this.$set(this.form, valObj.name, valObj.initValue);
});
},
computed: {
inlineClasses() {
return 'form form-inline';
},
},
methods: {
required,
requiredIf,
requiredUnless,
minLength,
maxLength,
minValue,
maxValue,
alpha,
alphaNum,
numeric,
decimal,
integer,
email,
ipAddress,
between,
url,
sentChange(bool) {
this.sent = bool;
},
},
};
</script>
<style lang="scss" scoped>
.submit {
display: flex;
justify-content: space-around;
margin: 0 -5px;
> * {
flex: 1;
margin: 0 5px;
}
}
.error-message {
color: red;
font-size: 13px;
padding-top: 3px;
}
.has-error {
.el-input__inner {
border-color: red;
}
}
</style>