Skip to content

Commit

Permalink
rewrite field validate
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Feb 21, 2017
1 parent 5b8bd23 commit 0f83624
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
27 changes: 17 additions & 10 deletions src/fields/abstractField.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export default {
"disabled"
],

data() {
return {
errors: []
};
},

computed: {
value: {
cache: false,
Expand Down Expand Up @@ -44,7 +50,7 @@ export default {
this.$emit("model-updated", newValue, this.schema.model);

if (isFunction(this.schema.onChanged)) {
this.schema.onChanged(this.model, newValue, oldValue, this.schema);
this.schema.onChanged.call(this, this.model, newValue, oldValue, this.schema);
}

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

methods: {
validate() {
validate(calledParent) {
this.clearValidationErrors();

if (this.schema.validator && this.schema.readonly !== true && this.disabled !== true) {
Expand All @@ -74,26 +80,27 @@ export default {
let err = validator(this.value, this.schema, this.model);
if (err) {
if (isArray(err))
Array.prototype.push.apply(this.schema.errors, err);
Array.prototype.push.apply(this.errors, err);
else if (isString(err))
this.schema.errors.push(err);
this.errors.push(err);
}
});

}

if (isFunction(this.schema.onValidated)) {
this.schema.onValidated(this.model, this.schema.errors, this.schema);
this.schema.onValidated.call(this, this.model, this.errors, this.schema);
}

return this.schema.errors;
let isValid = this.errors.length == 0;
if (!calledParent)
this.$emit("validated", isValid, this.errors, this);

return this.errors;
},

clearValidationErrors() {
if (isUndefined(this.schema.errors))
this.$root.$set(this.schema, "errors", []); // Be reactive
else
this.schema.errors.splice(0); // Clear
this.errors.splice(0);
},

setModelValueByPath(path, value) {
Expand Down
26 changes: 22 additions & 4 deletions src/formGenerator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ div
i.icon
.helpText(v-html='field.help')
.field-wrap
component(:is='getFieldType(field)', :disabled='fieldDisabled(field)', :model='model', :schema.sync='field', @model-updated='modelUpdated')
component(:is='getFieldType(field)', :disabled='fieldDisabled(field)', :model='model', :schema.sync='field', @model-updated='modelUpdated', @validated="onFieldValidated")
.buttons(v-if='buttonVisibility(field)')
button(v-for='btn in field.buttons', @click='btn.onclick(model, field)', :class='btn.classes') {{ btn.label }}
.hint(v-if='field.hint') {{ field.hint }}
Expand Down Expand Up @@ -205,15 +205,33 @@ div
return field.featured;
},
// Child field executed validation
onFieldValidated(res, errors, field) {
this.errors = this.errors.filter(e => e.field == field.schema);
// Remove old errors for this field
if (!res && errors && errors.length > 0) {
// Add errors with this field
errors.forEach((err) => {
this.errors.push({
field: field.schema,
error: err
});
});
}
let isValid = this.errors.length == 0;
this.$emit("validated", isValid, this.errors);
},
// Validating the model properties
validate() {
this.clearValidationErrors();
each(this.$children, (child) => {
this.$children.forEach((child) => {
if (isFunction(child.validate))
{
let err = child.validate();
each(err, (err) => {
let errors = child.validate(true);
errors.forEach((err) => {
this.errors.push({
field: child.schema,
error: err
Expand Down
12 changes: 6 additions & 6 deletions test/unit/specs/fields/abstractField.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ describe("abstractField.vue", function() {
expect(res[0]).to.be.equal("Validation error!");

expect(schema.onValidated.calledOnce).to.be.true;
expect(schema.onValidated.calledWith(model, field.schema.errors, schema)).to.be.true;
expect(schema.onValidated.calledWith(model, field.errors, schema)).to.be.true;
});

});
Expand All @@ -370,19 +370,19 @@ describe("abstractField.vue", function() {
});

it("should be undefined", () => {
expect(schema.errors).to.be.undefined;
expect(field.errors).to.be.an.array;
});

it("should be an empty array", () => {
field.clearValidationErrors();
expect(schema.errors).to.be.defined;
expect(schema.errors).to.be.length(0);
expect(field.errors).to.be.defined;
expect(field.errors).to.be.length(0);
});

it("should contain one error string", () => {
field.validate();
expect(schema.errors).to.be.length(1);
expect(schema.errors[0]).to.be.equal("Validation error!");
expect(field.errors).to.be.length(1);
expect(field.errors[0]).to.be.equal("Validation error!");
});

});
Expand Down

0 comments on commit 0f83624

Please sign in to comment.