Skip to content

Commit 369b69f

Browse files
committed
add on-validated event to formGenerator
1 parent 5ffcf20 commit 369b69f

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

src/formGenerator.vue

+3-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ div
222222
}
223223
});
224224
225-
return this.errors.length == 0;
225+
let isValid = this.errors.length == 0;
226+
this.$emit('on-validated', isValid, this.errors);
227+
return isValid;
226228
},
227229
228230
// Clear validation errors

test/unit/specs/VueFormGenerator.spec.js

+70
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,76 @@ describe("VueFormGenerator.vue", () => {
629629

630630
});
631631

632+
describe.only("check onValidated event", () => {
633+
let schema = {
634+
fields: [
635+
{
636+
type: "input",
637+
inputType: "text",
638+
label: "Name",
639+
model: "name",
640+
min: 3,
641+
validator: VueFormGenerator.validators.string
642+
}
643+
]
644+
};
645+
646+
let model = { name: "Bob" };
647+
let form;
648+
let onValidated = sinon.spy();
649+
650+
before( (done) => {
651+
let elm = document.createElement("div");
652+
vm = new Vue({
653+
// eslint-disable-next-line quotes
654+
template: `<vue-form-generator :schema="schema" :model="model" :options="options" :multiple="false" ref="form" @on-validated="onValidated"></vue-form-generator>`,
655+
data: {
656+
schema,
657+
model,
658+
options: {}
659+
},
660+
methods: {
661+
onValidated
662+
}
663+
}).$mount(elm);
664+
665+
el = vm.$el;
666+
vm.$nextTick( () => {
667+
form = vm.$refs.form;
668+
done();
669+
});
670+
});
671+
672+
it("should no errors after mounted()", (done) => {
673+
vm.$nextTick( () => {
674+
expect(form.errors).to.be.length(0);
675+
done();
676+
});
677+
});
678+
679+
it("should be validation error if model value is not valid", () => {
680+
vm.model.name = "A";
681+
onValidated.reset();
682+
form.validate();
683+
684+
expect(form.errors).to.be.length(1);
685+
expect(onValidated.callCount).to.be.equal(1);
686+
// console.log(onValidated.getCall(0).args[1][0].field);
687+
// console.log(schema.fields[0]);
688+
expect(onValidated.calledWith(false, [{ field: schema.fields[0], error: "The length of text is too small! Current: 1, Minimum: 3"}] )).to.be.true;
689+
});
690+
691+
it("should no validation error if model valie is valid", () => {
692+
vm.model.name = "Alan";
693+
onValidated.reset();
694+
form.validate();
695+
696+
expect(form.errors).to.be.length(0);
697+
expect(onValidated.callCount).to.be.equal(1);
698+
expect(onValidated.calledWith(true, [] )).to.be.true;
699+
});
700+
});
701+
632702
describe("check schema.onChanged when the model changed", () => {
633703
let schema = {
634704
fields: [

test/unit/webpack.test.config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ module.exports = {
6868
autoprefixer: {
6969
browsers: ["last 2 versions"]
7070
},
71-
loaders: {
71+
// Comment this, if you would like to debug under `npm run ci`
72+
/*loaders: {
7273
js: "isparta"
73-
}
74+
}*/
7475
}
7576

7677
};

0 commit comments

Comments
 (0)