From 4dc8c2d1cf0e41bc110e729d94a406a15989cf89 Mon Sep 17 00:00:00 2001 From: David Higgins Date: Fri, 14 Dec 2018 11:41:24 -0500 Subject: [PATCH] added docs for "Vue.use" options introduced in https://github.com/vue-generators/vue-form-generator/pull/560 --- usage.md | 6 ++++++ validation/README.md | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/usage.md b/usage.md index 5bd9ea1..c7a2d2e 100644 --- a/usage.md +++ b/usage.md @@ -19,6 +19,12 @@ import VueFormGenerator from "vue-form-generator"; Vue.use(VueFormGenerator); +/* optional custom validators treated as "built-in" +Vue.use(VueFormGenerator, { + validators: objectWithCustomValidatorFunctions +}); +*/ + export default { data () { return { diff --git a/validation/README.md b/validation/README.md index 81bcc38..1c64d2f 100644 --- a/validation/README.md +++ b/validation/README.md @@ -115,7 +115,39 @@ or function names: ## Custom Validators -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). +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). + +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). + +### Example of Custom Validators being Installed + +```js +import VueFormGenerator from "vue-form-generator"; +Vue.use(VueFormGenerator, { + validators: { + firstCustomValidator: (value, field, model) => { + return []; + }, + secondCustomValidator: (value, field, model) => { + return []; + }, + alwaysInvalid: (value, field, model) => { + return ['I am always invalid']; + } + } +}); +``` + +You can then reference these in your field schema by their name: + +``` +{ + type: "input", + inputType: "text", + model: "model", + validator: ["firstCustomValidator", "secondCustomValidator", "alwaysInvalid"] +} +``` ## Handling Validation Events