forked from vue-generators/vue-form-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstractField.js
171 lines (142 loc) · 3.82 KB
/
abstractField.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { get as objGet, each, identity, isFunction, isString, isArray } from "lodash";
import validators from "../utils/validators";
import { slugifyFormID } from "../utils/schema";
function convertValidator(validator) {
if (isString(validator)) {
if (validators[validator] != null)
return validators[validator];
else {
console.warn(`'${validator}' is not a validator function!`);
return null; // caller need to handle null
}
}
return validator;
}
export default {
props: [
"model",
"schema",
"formOptions",
"disabled"
],
data() {
return {
errors: []
};
},
computed: {
value: {
cache: false,
get() {
let val;
if (isFunction(this.schema.get))
val = this.schema.get(this.model);
else if (this.model && this.schema.model)
val = objGet(this.model, this.schema.model);
return this.formatValueToField(val);
},
set(newValue) {
let oldValue = this.value;
newValue = this.formatValueToModel(newValue);
let changed = false;
if (isFunction(this.schema.set)) {
this.schema.set(this.model, newValue);
changed = true;
} else if (this.schema.model) {
this.setModelValueByPath(this.schema.model, newValue);
changed = true;
}
if (changed) {
this.$emit("model-updated", newValue, this.schema.model);
if (isFunction(this.schema.onChanged)) {
this.schema.onChanged.call(this, this.model, newValue, oldValue, this.schema);
}
if (this.$parent.options && this.$parent.options.validateAfterChanged === true){
this.validate();
}
}
}
}
},
methods: {
formatValueToField: identity,
formatValueToModel: identity,
validate(calledParent) {
this.clearValidationErrors();
if (this.schema.validator && this.schema.readonly !== true && this.disabled !== true) {
let validators = [];
if (!isArray(this.schema.validator)) {
validators.push(convertValidator(this.schema.validator).bind(this));
} else {
each(this.schema.validator, (validator) => {
validators.push(convertValidator(validator).bind(this));
});
}
each(validators, (validator) => {
let addErrors = err => {
if (isArray(err))
Array.prototype.push.apply(this.errors, err);
else if (isString(err))
this.errors.push(err);
};
let res = validator(this.value, this.schema, this.model);
if (res && isFunction(res.then)) {
// It is a Promise, async validator
res.then(err => {
if (err) {
addErrors(err);
let isValid = this.errors.length == 0;
this.$emit("validated", isValid, this.errors, this);
}
});
} else {
if (res)
addErrors(res);
}
});
}
if (isFunction(this.schema.onValidated)) {
this.schema.onValidated.call(this, this.model, this.errors, this.schema);
}
let isValid = this.errors.length == 0;
if (!calledParent)
this.$emit("validated", isValid, this.errors, this);
return this.errors;
},
clearValidationErrors() {
this.errors.splice(0);
},
setModelValueByPath(path, value) {
// convert array indexes to properties
let s = path.replace(/\[(\w+)\]/g, ".$1");
// strip a leading dot
s = s.replace(/^\./, "");
let o = this.model;
const a = s.split(".");
let i = 0;
const n = a.length;
while (i < n) {
let k = a[i];
if (i < n - 1)
if (o[k] !== undefined) {
// Found parent property. Step in
o = o[k];
} else {
// Create missing property (new level)
this.$root.$set(o, k, {});
o = o[k];
}
else {
// Set final property value
this.$root.$set(o, k, value);
return;
}
++i;
}
},
getFieldID(schema) {
const idPrefix = this.formOptions && this.formOptions.fieldIdPrefix ? this.formOptions.fieldIdPrefix : "";
return slugifyFormID(schema, idPrefix);
}
}
};