Skip to content

Commit 82d4ee8

Browse files
author
cristian.jora
committed
Merge branch 'master' of https://github.com/icebob/vue-form-generator into file-upload
# Conflicts: # dist/vfg-core.js # dist/vfg.js
2 parents 4ac96e4 + 1f99ab6 commit 82d4ee8

File tree

9 files changed

+307
-108
lines changed

9 files changed

+307
-108
lines changed

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,27 @@ A schema-based form generator component for Vue.js.
1717
[![Screenshot](https://icebob.gitbooks.io/vueformgenerator/content/assets/vfg-example1.png)](https://jsfiddle.net/icebob/0mg1v81e/)
1818

1919
## Features
20-
- multiple objects editing
21-
- core & full bundles
20+
- reactive forms based on schemas
21+
- multiple object editing
2222
- 21 field types
2323
- built-in validators
24+
- core & full bundles (11kb and 19kb gzipped)
2425
- Bootstrap friendly templates
2526
- customizable styles
26-
- extendable with custom fields
27+
- can be extended easily with custom fields
2728
- ...etc
2829

2930
## Documentation
3031
[Online documentation on Gitbook](https://icebob.gitbooks.io/vueformgenerator/content/)
3132

3233
## Dependencies
33-
vue-form-generator use [fecha](https://github.com/taylorhakes/fecha) and [lodash](https://lodash.com/) internally.
34+
vue-form-generator uses [fecha](https://github.com/taylorhakes/fecha) and [lodash](https://lodash.com/) internally.
3435

3536
While built-in fields don't need external dependencies, optional fields may need other libraries.
36-
These dependency fall in two camp: jQuery or Vanilla. You can find almost the same functionality in both flavor.
37-
That way, it's your choice to depend on jQuery or not.
37+
These dependencies fall into two camps: jQuery or Vanilla. You can find almost the same functionality in both flavors.
38+
In the end, it's your choice to depend on jQuery or not.
3839

39-
You can find details about dependencies in [documentation](https://icebob.gitbooks.io/vueformgenerator/content/).
40+
You can find details about dependencies in the official [documentation](https://icebob.gitbooks.io/vueformgenerator/content/) under each specific component.
4041

4142
## Installation
4243
### NPM
@@ -156,6 +157,17 @@ export default {
156157
</script>
157158
```
158159

160+
Usage in local components
161+
```js
162+
import VueFormGenerator from "vue-form-generator";
163+
164+
//component javascript
165+
export default{
166+
components:{
167+
"vue-form-generator": VueFormGenerator.component
168+
}
169+
}
170+
```
159171
## Development
160172
This command will start a `webpack-dev-server` with content of `dev` folder.
161173
```bash
@@ -178,7 +190,7 @@ npm run ci
178190
```
179191

180192
## More fields *new*
181-
VueFormGenerator support custom fields. If you decide to release your custom field into the wild, please open a new issue so we can add you to a list here! Please try to use this naming convention for your custom field : vfg-field-* Example :
193+
VueFormGenerator supports custom fields. If you decide to release your custom field into the wild, please open a new issue so we can add you to a list here! Please try to use this naming convention for your custom field : vfg-field-* Example :
182194

183195
- `vfg-field-myfield`
184196
- `vfg-field-calendar`

dev/full/schema.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import {
44
validators
55
} from "../../src";
66

7+
let customAsyncValidator = function(value) {
8+
return new Promise((resolve, reject) => {
9+
setTimeout(() => {
10+
if (value)
11+
resolve();
12+
else
13+
resolve([ "Invalid value from async validator" ]);
14+
}, 1000);
15+
});
16+
};
17+
718
module.exports = {
819
fields: [
920

@@ -56,7 +67,7 @@ module.exports = {
5667
model: "website",
5768
placeholder: "Enter your website",
5869
inputName: "website",
59-
validator: validators.url
70+
validator: customAsyncValidator //validators.url
6071
}, {
6172
type: "input",
6273
inputType: "tel",
@@ -74,7 +85,10 @@ module.exports = {
7485
required: true,
7586
hint: "Minimum 6 characters",
7687
styleClasses: "half-width",
77-
validator: validators.string
88+
validator: validators.string.locale({
89+
fieldIsRequired: "The password is required!",
90+
textTooSmall: "Password must be at least {1} characters!"
91+
})
7892
}, {
7993
type: "input",
8094
inputType: "date",
@@ -117,8 +131,8 @@ module.exports = {
117131
inputType: "number",
118132
label: "Number",
119133
model: "age",
120-
styleClasses: "half-width",
121-
validator: validators.number
134+
styleClasses: "half-width"
135+
//validator: validators.number
122136
}, {
123137
type: "input",
124138
inputType: "range",
@@ -309,7 +323,8 @@ module.exports = {
309323
rows: 4,
310324
validator: validators.string
311325
}, {
312-
type: "text",
326+
type: "input",
327+
inputType: "text",
313328
label: "Field with buttons",
314329
model: "address.geo",
315330
disabled: false,

src/fields/abstractField.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
import { get as objGet, each, isFunction, isString, isArray } from "lodash";
2+
import validators from "../utils/validators";
3+
4+
function convertValidator(validator) {
5+
if (isString(validator)) {
6+
if (validators[validator] != null)
7+
return validators[validator];
8+
else {
9+
console.warn(`'${validator}' is not a validator function!`);
10+
return null; // caller need to handle null
11+
}
12+
}
13+
return validator;
14+
}
215

316
export default {
417
props: [
@@ -69,20 +82,34 @@ export default {
6982

7083
let validators = [];
7184
if (!isArray(this.schema.validator)) {
72-
validators.push(this.schema.validator.bind(this));
85+
validators.push(convertValidator(this.schema.validator).bind(this));
7386
} else {
7487
each(this.schema.validator, (validator) => {
75-
validators.push(validator.bind(this));
88+
validators.push(convertValidator(validator).bind(this));
7689
});
7790
}
7891

7992
each(validators, (validator) => {
80-
let err = validator(this.value, this.schema, this.model);
81-
if (err) {
93+
let addErrors = err => {
8294
if (isArray(err))
8395
Array.prototype.push.apply(this.errors, err);
8496
else if (isString(err))
8597
this.errors.push(err);
98+
};
99+
100+
let res = validator(this.value, this.schema, this.model);
101+
if (res && isFunction(res.then)) {
102+
// It is a Promise, async validator
103+
res.then(err => {
104+
if (err) {
105+
addErrors(err);
106+
let isValid = this.errors.length == 0;
107+
this.$emit("validated", isValid, this.errors, this);
108+
}
109+
});
110+
} else {
111+
if (res)
112+
addErrors(res);
86113
}
87114
});
88115

src/fields/core/fieldChecklist.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
.listbox.form-control(v-if="schema.listBox", :disabled="disabled")
44
.list-row(v-for="item in items", :class="{'is-checked': isItemChecked(item)}")
55
label
6-
input(type="checkbox", :checked="isItemChecked(item)", :disabled="disabled", @input="onChanged($event, item)")
6+
input(type="checkbox", :checked="isItemChecked(item)", :disabled="disabled", @change="onChanged($event, item)")
77
| {{ getItemName(item) }}
88

99
.combobox.form-control(v-if="!schema.listBox", :disabled="disabled")
@@ -14,7 +14,7 @@
1414
.dropList
1515
.list-row(v-if="comboExpanded", v-for="item in items", :class="{'is-checked': isItemChecked(item)}")
1616
label
17-
input(type="checkbox", :checked="isItemChecked(item)", :disabled="disabled", @input="onChanged($event, item)")
17+
input(type="checkbox", :checked="isItemChecked(item)", :disabled="disabled", @change="onChanged($event, item)")
1818
| {{ getItemName(item) }}
1919
</template>
2020

0 commit comments

Comments
 (0)