Skip to content

Commit 007c592

Browse files
matt-sandersmohammedzamakhan
authored andcommitted
Fixes #1 (#2)
* beginning v2 build * FormlyForm ready for 2.0 * working on the FormlyField component * working on field validation * added template options * build
1 parent ea52e1d commit 007c592

File tree

8 files changed

+5500
-9578
lines changed

8 files changed

+5500
-9578
lines changed

dist/vue-formly.js

Lines changed: 5066 additions & 9205 deletions
Large diffs are not rendered by default.

dist/vue-formly.min.js

Lines changed: 6 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-formly",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "A simple and extendable form builder for Vue.js",
55
"main": "dist/vue-formly.js",
66
"scripts": {
@@ -9,7 +9,7 @@
99
},
1010
"repository": {
1111
"type": "git",
12-
"url": "git+https://github.com/matt-sanders/vue-formly.git"
12+
"url": "git+https://github.com/formly-js/vue-formly.git"
1313
},
1414
"keywords": [
1515
"vue",
@@ -18,9 +18,12 @@
1818
],
1919
"license": "MIT",
2020
"bugs": {
21-
"url": "https://github.com/matt-sanders/vue-formly/issues"
21+
"url": "https://github.com/formly-js/vue-formly/issues"
22+
},
23+
"homepage": "https://github.com/formly-js/vue-formly#readme",
24+
"peerDependencies": {
25+
"vue": "2.x"
2226
},
23-
"homepage": "https://github.com/matt-sanders/vue-formly#readme",
2427
"devDependencies": {
2528
"babel-core": "6.11.4",
2629
"babel-loader": "6.2.4",
@@ -71,7 +74,6 @@
7174
"sinon-chai": "2.8.0",
7275
"style-loader": "0.13.1",
7376
"uglify-js": "^2.6.4",
74-
"vue": "1.0.26",
7577
"vue-hot-reload-api": "1.3.2",
7678
"vue-html-loader": "1.2.3",
7779
"vue-loader": "8.5.3",

src/components/FormlyField.vue

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,56 @@
11
<template>
2-
<component :is="type" :form.sync="form" :key="key"></component>
2+
<component :is="type" :form.sync="form" :field="field" :model="model" :to="field.templateOptions"></component>
33
</template>
44

55
<script>
66
const Vue = require('vue');
77
import Util, {getTypes, setError} from '../util';
88
export default {
9-
props: ['form', 'key'],
10-
computed: {
11-
type:function(){
12-
return 'formly_'+this.form[this.key].type;
13-
}
14-
},
15-
methods: {
16-
validate:function(){
17-
let field = this.form[this.key];
9+
props: ['form', 'model', 'field', 'to'],
10+
computed: {
11+
type:function(){
12+
return 'formly_'+this.field.type;
13+
}
14+
},
15+
methods: {
16+
validate:function(){
1817
19-
//first check if we need to create a field
20-
if ( !this.form.$errors[this.key] ) this.$set('form.$errors.'+this.key, {});
18+
//first check if we need to create a field
19+
if ( !this.form.$errors[this.field.key] ) this.$set(this.form.$errors, this.field.key, {});
20+
if ( !this.field.templateOptions ) this.$set(this.field, 'templateOptions', {});
2121
22-
//check for required fields. This whole setting,unsetting thing seems kind of wrong though..
23-
//there might be a more 'vue-ey' way to do this...
24-
if ( field.required ){
25-
if ( !this.form.$errors[this.key].required ) this.$set('form.$errors.'+this.key+'.required', true);
26-
setError(this.form, this.key, 'required', !field.value) ;
27-
}
22+
//check for required fields. This whole setting,unsetting thing seems kind of wrong though..
23+
//there might be a more 'vue-ey' way to do this...
24+
if ( this.field.required ){
25+
if ( !this.form.$errors[this.field.key].required ) this.$set(this.form.$errors[ this.field.key ], 'required', true);
26+
setError(this.form, this.field.key, 'required', !this.model[ this.field.key ]) ;
27+
}
2828
29-
//if we've got nothing left then return
30-
if ( !field.validators ) return;
29+
//if we've got nothing left then return
30+
if ( !this.field.validators ) return;
3131
32-
Object.keys(field.validators).forEach((validKey) => {
33-
if ( !this.form.$errors[this.key][validKey] ) this.$set('form.$errors.'+this.key+'.'+validKey, false);
34-
if ( !field.required && !field.value ) return;
32+
//set these for the validators so we don't have to use 'this' in them
33+
let model = this.model;
34+
let field = this.field;
35+
36+
Object.keys(this.field.validators).forEach((validKey) => {
37+
if ( !this.form.$errors[this.field.key][validKey] ) this.$set(this.form.$errors[ this.field.key ], validKey, false);
38+
if ( !this.field.required && !this.model[ this.field.key ] ) return;
3539
36-
let validator = field.validators[validKey];
40+
let validator = this.field.validators[validKey];
3741
38-
let valid = typeof validator == 'function' ? !validator(field) : !eval(validator);
39-
setError(this.form, this.key, validKey, valid);
40-
41-
});
42-
}
43-
},
44-
components: getTypes(),
45-
created(){
46-
this.validate();
47-
this.$watch('form.'+this.key+'.value', (val) =>{
48-
let valid = this.validate();
49-
});
42+
let valid = typeof validator == 'function' ? !validator(field, model) : !eval(validator);
43+
setError(this.form, this.field.key, validKey, valid);
44+
45+
});
5046
}
51-
}
47+
},
48+
components: getTypes(),
49+
created(){
50+
this.validate();
51+
this.$watch('model.'+this.field.key, (val) =>{
52+
let valid = this.validate();
53+
});
54+
}
55+
}
5256
</script>

src/components/FormlyForm.vue

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
11
<template>
22
<fieldset>
3-
<template v-if="!customLayout">
4-
<formly-field v-for="field in form | formlyFields" :form.sync="form" :key="field" ></formly-field>
5-
</template>
3+
<formly-field v-for="field in fields" :form.sync="form" :model.sync="model" :field="field"></formly-field>
64
<slot></slot>
75
</fieldset>
86
</template>
97

108
<script>
119
export default {
12-
props: ['form', 'customLayout'],
13-
created(){
14-
//make sure that the 'value' is always set
15-
Object.keys(this.form).forEach((key) => {
16-
if ( typeof this.form[key].value == 'undefined' ) this.$set('form.'+key+'.value', '');
17-
});
18-
19-
//set our validation options
20-
this.$set('form.$errors', {});
21-
this.$set('form.$valid', true);
10+
props: ['form', 'model', 'fields'],
11+
created(){
2212
23-
this.$watch('form.$errors', function(val){
24-
let valid = true;
25-
Object.keys(this.form.$errors).forEach((key)=>{
26-
let errField = this.form.$errors[key];
27-
Object.keys(errField).forEach((errKey) => {
28-
if ( errField[errKey] ) valid = false;
29-
})
30-
});
31-
this.form.$valid = valid;
32-
}, {
33-
deep: true
34-
});
35-
}
13+
//make sure that the 'value' is always set
14+
this.fields.forEach( field => {
15+
if ( typeof this.model[ field.key ] == 'undefined' ) this.$set(this.model, field.key, '');
16+
});
17+
18+
19+
//set our validation options
20+
this.$set(this.form, '$errors', {});
21+
this.$set(this.form, '$valid', true);
22+
23+
this.$watch('form.$errors', function(val){
24+
let valid = true;
25+
Object.keys(this.form.$errors).forEach((key)=>{
26+
let errField = this.form.$errors[key];
27+
Object.keys(errField).forEach((errKey) => {
28+
if ( errField[errKey] ) valid = false;
29+
})
30+
});
31+
this.form.$valid = valid;
32+
}, {
33+
deep: true
34+
});
35+
36+
}
3637
}
3738
</script>

0 commit comments

Comments
 (0)