Skip to content

Commit 2241ed9

Browse files
authored
Merge pull request #4 from vue-generators/feature/vue-use-options-validators
added docs for "Vue.use" options
2 parents 966254b + 4dc8c2d commit 2241ed9

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

usage.md

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ import VueFormGenerator from "vue-form-generator";
1919
2020
Vue.use(VueFormGenerator);
2121
22+
/* optional custom validators treated as "built-in"
23+
Vue.use(VueFormGenerator, {
24+
validators: objectWithCustomValidatorFunctions
25+
});
26+
*/
27+
2228
export default {
2329
data () {
2430
return {

validation/README.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,39 @@ or function names:
115115

116116
## Custom Validators
117117

118-
You can create custom validators, which are just functions that take `value`, `field`, `model` parameters - and return either an array of errors, or an empty array if validation succeeds. [Full details on creating custom validators can be found here](custom-validators.md).
118+
You can create custom validators, which are functions that take `value`, `field`, `model` parameters - and return either an array of errors, or an empty array if validation succeeds. [Full details on creating custom validators can be found here](custom-validators.md).
119+
120+
If your custom validators are attached to a single object, you can pass this object to `Vue.use()` when setting up VFG in your project. This will take each function in the object passed and attach it to the list of built-in validators. This allows you to reference your custom validators as "name strings" (convenient for JSON schemas retrieved remotely).
121+
122+
### Example of Custom Validators being Installed
123+
124+
```js
125+
import VueFormGenerator from "vue-form-generator";
126+
Vue.use(VueFormGenerator, {
127+
validators: {
128+
firstCustomValidator: (value, field, model) => {
129+
return [];
130+
},
131+
secondCustomValidator: (value, field, model) => {
132+
return [];
133+
},
134+
alwaysInvalid: (value, field, model) => {
135+
return ['I am always invalid'];
136+
}
137+
}
138+
});
139+
```
140+
141+
You can then reference these in your field schema by their name:
142+
143+
```
144+
{
145+
type: "input",
146+
inputType: "text",
147+
model: "model",
148+
validator: ["firstCustomValidator", "secondCustomValidator", "alwaysInvalid"]
149+
}
150+
```
119151

120152
## Handling Validation Events
121153

0 commit comments

Comments
 (0)