Skip to content

Commit 1cab4a6

Browse files
committed
updated form validity checking
1 parent 89c0288 commit 1cab4a6

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/components/FormlyForm.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@
1515
1616
//set our validation options
1717
this.$set('form.$errors', {});
18-
this.$set('form.$valid', false);
18+
this.$set('form.$valid', true);
19+
20+
this.$watch('form.$errors', (val) => {
21+
let valid = false;
22+
if ( Object.keys(this.form.$errors).length == 0 ) valid = true;
23+
this.form.$valid = valid;
24+
return valid;
25+
});
1926
}
2027
}
2128
</script>

test/unit/specs/FormlyForm.spec.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('FormlyForm', () => {
6767
expect(JSON.parse(vm.$el.querySelector('#lname_field').textContent)).to.deep.equal(data.form.lname);
6868
expect(JSON.parse(vm.$el.querySelector('#fname_field').textContent)).to.deep.equal({type: 'input', value: ''});
6969
expect(data.form.$errors).to.deep.equal({});
70-
expect(data.form.$valid).to.be.false;
70+
expect(data.form.$valid).to.be.true;
7171

7272
});
7373

@@ -101,5 +101,22 @@ describe('FormlyForm', () => {
101101
expect(vm.$el.querySelectorAll('fieldset .restricted-field')).to.be.length(1);
102102

103103
});
104+
105+
it('should compute any errors', (done) => {
106+
let data = {
107+
form: {
108+
109+
}
110+
};
111+
createForm('<formly-form :form="form"></formly-form>', data);
112+
expect(vm.form.$errors).to.deep.equal({});
113+
expect(vm.form.$valid).to.be.true;
114+
vm.$set('form.$errors.test', 'testing');
115+
116+
setTimeout(()=>{
117+
expect(vm.form.$valid).to.be.false;
118+
done();
119+
},0);
120+
});
104121

105122
});

0 commit comments

Comments
 (0)