Skip to content

Commit 0f83624

Browse files
committed
rewrite field validate
1 parent 5b8bd23 commit 0f83624

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

src/fields/abstractField.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ export default {
77
"disabled"
88
],
99

10+
data() {
11+
return {
12+
errors: []
13+
};
14+
},
15+
1016
computed: {
1117
value: {
1218
cache: false,
@@ -44,7 +50,7 @@ export default {
4450
this.$emit("model-updated", newValue, this.schema.model);
4551

4652
if (isFunction(this.schema.onChanged)) {
47-
this.schema.onChanged(this.model, newValue, oldValue, this.schema);
53+
this.schema.onChanged.call(this, this.model, newValue, oldValue, this.schema);
4854
}
4955

5056
if (this.$parent.options && this.$parent.options.validateAfterChanged === true){
@@ -56,7 +62,7 @@ export default {
5662
},
5763

5864
methods: {
59-
validate() {
65+
validate(calledParent) {
6066
this.clearValidationErrors();
6167

6268
if (this.schema.validator && this.schema.readonly !== true && this.disabled !== true) {
@@ -74,26 +80,27 @@ export default {
7480
let err = validator(this.value, this.schema, this.model);
7581
if (err) {
7682
if (isArray(err))
77-
Array.prototype.push.apply(this.schema.errors, err);
83+
Array.prototype.push.apply(this.errors, err);
7884
else if (isString(err))
79-
this.schema.errors.push(err);
85+
this.errors.push(err);
8086
}
8187
});
8288

8389
}
8490

8591
if (isFunction(this.schema.onValidated)) {
86-
this.schema.onValidated(this.model, this.schema.errors, this.schema);
92+
this.schema.onValidated.call(this, this.model, this.errors, this.schema);
8793
}
8894

89-
return this.schema.errors;
95+
let isValid = this.errors.length == 0;
96+
if (!calledParent)
97+
this.$emit("validated", isValid, this.errors, this);
98+
99+
return this.errors;
90100
},
91101

92102
clearValidationErrors() {
93-
if (isUndefined(this.schema.errors))
94-
this.$root.$set(this.schema, "errors", []); // Be reactive
95-
else
96-
this.schema.errors.splice(0); // Clear
103+
this.errors.splice(0);
97104
},
98105

99106
setModelValueByPath(path, value) {

src/formGenerator.vue

+22-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ div
99
i.icon
1010
.helpText(v-html='field.help')
1111
.field-wrap
12-
component(:is='getFieldType(field)', :disabled='fieldDisabled(field)', :model='model', :schema.sync='field', @model-updated='modelUpdated')
12+
component(:is='getFieldType(field)', :disabled='fieldDisabled(field)', :model='model', :schema.sync='field', @model-updated='modelUpdated', @validated="onFieldValidated")
1313
.buttons(v-if='buttonVisibility(field)')
1414
button(v-for='btn in field.buttons', @click='btn.onclick(model, field)', :class='btn.classes') {{ btn.label }}
1515
.hint(v-if='field.hint') {{ field.hint }}
@@ -205,15 +205,33 @@ div
205205
return field.featured;
206206
},
207207
208+
// Child field executed validation
209+
onFieldValidated(res, errors, field) {
210+
this.errors = this.errors.filter(e => e.field == field.schema);
211+
// Remove old errors for this field
212+
if (!res && errors && errors.length > 0) {
213+
// Add errors with this field
214+
errors.forEach((err) => {
215+
this.errors.push({
216+
field: field.schema,
217+
error: err
218+
});
219+
});
220+
}
221+
222+
let isValid = this.errors.length == 0;
223+
this.$emit("validated", isValid, this.errors);
224+
},
225+
208226
// Validating the model properties
209227
validate() {
210228
this.clearValidationErrors();
211229
212-
each(this.$children, (child) => {
230+
this.$children.forEach((child) => {
213231
if (isFunction(child.validate))
214232
{
215-
let err = child.validate();
216-
each(err, (err) => {
233+
let errors = child.validate(true);
234+
errors.forEach((err) => {
217235
this.errors.push({
218236
field: child.schema,
219237
error: err

test/unit/specs/fields/abstractField.spec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ describe("abstractField.vue", function() {
351351
expect(res[0]).to.be.equal("Validation error!");
352352

353353
expect(schema.onValidated.calledOnce).to.be.true;
354-
expect(schema.onValidated.calledWith(model, field.schema.errors, schema)).to.be.true;
354+
expect(schema.onValidated.calledWith(model, field.errors, schema)).to.be.true;
355355
});
356356

357357
});
@@ -370,19 +370,19 @@ describe("abstractField.vue", function() {
370370
});
371371

372372
it("should be undefined", () => {
373-
expect(schema.errors).to.be.undefined;
373+
expect(field.errors).to.be.an.array;
374374
});
375375

376376
it("should be an empty array", () => {
377377
field.clearValidationErrors();
378-
expect(schema.errors).to.be.defined;
379-
expect(schema.errors).to.be.length(0);
378+
expect(field.errors).to.be.defined;
379+
expect(field.errors).to.be.length(0);
380380
});
381381

382382
it("should contain one error string", () => {
383383
field.validate();
384-
expect(schema.errors).to.be.length(1);
385-
expect(schema.errors[0]).to.be.equal("Validation error!");
384+
expect(field.errors).to.be.length(1);
385+
expect(field.errors[0]).to.be.equal("Validation error!");
386386
});
387387

388388
});

0 commit comments

Comments
 (0)