Skip to content

Commit 124b571

Browse files
committed
added required checks
1 parent 1cab4a6 commit 124b571

File tree

4 files changed

+112
-3
lines changed

4 files changed

+112
-3
lines changed

src/components/FormlyField.vue

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,36 @@
44

55
<script>
66
const Vue = require('vue');
7-
import Util, {getTypes} from '../util';
7+
import Util, {getTypes, addError, removeError} from '../util';
88
export default {
99
props: ['form', 'key'],
1010
computed: {
1111
type:function(){
1212
return 'formly_'+this.form[this.key].type;
1313
}
1414
},
15-
components: getTypes()
15+
methods: {
16+
validate:function(){
17+
let field = this.form[this.key];
18+
19+
//just return straight away if nothing is there
20+
if ( !field.required && !field.value ) return;
21+
22+
if ( field.required ){
23+
if ( !field.value ){
24+
addError(this.form, this.key, 'required');
25+
} else {
26+
removeError(this.form, this.key, 'required');
27+
}
28+
}
29+
}
30+
},
31+
components: getTypes(),
32+
created(){
33+
this.validate();
34+
this.$watch('form.'+this.key+'.value', (val) =>{
35+
this.validate();
36+
});
37+
}
1638
}
1739
</script>

src/util.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,25 @@ export function addType(id, options){
1717
export function getTypes(){
1818
return exports.formlyFields;
1919
}
20+
21+
/**
22+
* Allows a field to add errors to the form
23+
* @param {Object} form
24+
* @param {String} key
25+
* @param {String} err
26+
*/
27+
export function addError(form, key, err){
28+
if ( !form.$errors[key] ) form.$errors[key] = {};
29+
form.$errors[key][err] = true;
30+
}
31+
32+
/**
33+
* Allows a field to remove an error from the form
34+
* @param {Object} form
35+
* @param {String} key
36+
* @param {String} err
37+
*/
38+
export function removeError(form, key, err){
39+
delete form.$errors[key][err];
40+
if (Object.keys(form.$errors[key]).length == 0 ) delete form.$errors[key];
41+
}

test/unit/specs/FormlyField.spec.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,57 @@ describe('FormlyField', () => {
7575
}, 0);
7676

7777
});
78+
79+
describe('Validation', ()=>{
80+
81+
before(()=>{
82+
Vue.component('formly_test', {
83+
props: ['form', 'key'],
84+
template: '<div></div>'
85+
});
86+
});
87+
88+
function createValidField(data){
89+
return createForm('<formly-field :form.sync="form" key="search"></formly-field>', data);
90+
};
91+
92+
it('should handle required values', (done) => {
93+
94+
let data = {
95+
form: {
96+
$valid: true,
97+
$errors: {},
98+
search: {
99+
type: 'test',
100+
value: '',
101+
required: true
102+
}
103+
}
104+
};
105+
106+
createValidField(data);
107+
expect(vm.form.$errors.search.required).to.be.true;
108+
109+
vm.$set('form.search.value','testing');
110+
setTimeout(()=>{
111+
expect(vm.form.$errors.search).to.equal(undefined);
112+
done();
113+
},0);
114+
115+
});
116+
117+
it('should take an expression', () => {
118+
let data = {
119+
form: {
120+
$valid: true,
121+
$errors: {},
122+
search: {
123+
124+
}
125+
}
126+
};
127+
});
128+
129+
});
78130

79131
});

test/unit/specs/index.spec.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {expect} from 'chai';
22

33
import VueFormly from 'src/index';
4-
import Util, {addType, getTypes} from 'src/util';
4+
import Util, {addType, getTypes, addError, removeError} from 'src/util';
55

66

77
describe('module', () => {
@@ -13,6 +13,8 @@ describe('module', () => {
1313
expect(VueFormly.getTypes).to.be.a('function');
1414
expect(addType).to.be.a('function');
1515
expect(getTypes).to.be.a('function');
16+
expect(addError).to.be.a('function');
17+
expect(removeError).to.be.a('function');
1618
expect(Util.formlyFields).to.be.a('object');
1719
});
1820

@@ -31,5 +33,16 @@ describe('module', () => {
3133
addType('test', newField);
3234
expect(getTypes().formly_test).to.deep.equal(newField);
3335
});
36+
37+
it('should handle errors',()=>{
38+
let form = {
39+
'$errors': {}
40+
};
41+
addError(form, 'fname', 'required');
42+
expect(form.$errors.fname.required).to.be.true;
43+
44+
removeError(form, 'fname', 'required');
45+
expect(typeof form.$errors.fname).to.equal('undefined');
46+
});
3447

3548
});

0 commit comments

Comments
 (0)