You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: essentials/validation.md
+217
Original file line number
Diff line number
Diff line change
@@ -728,3 +728,220 @@ layout: "auto"
728
728
## Custom rules
729
729
730
730
Validation rules are functions that accept a [core node](/essentials/architecture#node) and return a boolean value — `true` for passing and `false` for failing. Additionally, any arguments passed to the validation rule are available as arguments `1-n`. Writing your own is straight forward — for example:
731
+
732
+
```js
733
+
/**
734
+
* File: my-custom-rules/monday.js
735
+
*
736
+
* A contrived validation rule that ensures the input’s value is monday or mon.
737
+
*/
738
+
constmonday=function (node) {
739
+
returnnode.value==='monday'||node.value==='mon'
740
+
}
741
+
742
+
exportdefaultmonday
743
+
```
744
+
745
+
### Defining custom rule behaviors
746
+
747
+
As mentioned in the [validation rule hints](#rule-hints) section, validation rules — including your custom rules — operate according to default behaviors: they run in sequence, are skipped when the input's value is empty, are synchronous, and are blocking. If you want your rule's defaults to operate differently, you can override these on your custom validation rule:
748
+
749
+
```js
750
+
/**
751
+
* A contrived validation rule that ensures the input’s value is monday or mon.
752
+
*/
753
+
constmonday=function (node) {
754
+
returnnode.value==='monday'||node.value==='mon'
755
+
}
756
+
757
+
// override default rule behaviors for your custom rule
758
+
monday.blocking=false
759
+
monday.skipEmpty=false
760
+
monday.debounce=20// milliseconds
761
+
monday.force=true
762
+
763
+
exportdefaultmonday
764
+
```
765
+
766
+
You can also override these behaviors on a case-by-case basis with [rule hints](#rule-hints).
767
+
768
+
Once you have a validation function written — you need to register the validation rule with FormKit — either globally or specifically on an input.
769
+
770
+
### Multi-input validation rules
771
+
772
+
Validation rules can depend on values from other inputs in your [form’s tree](/essentials/architecture). To do so, use node traversal to locate another node and access its value:
Your custom rules probably need a custom message — the next section of the docs will cover that.
833
+
::
834
+
835
+
## Custom messages
836
+
837
+
There are several ways to customize your validation message. The most basic of which is to use the <code>validation-label</code> prop — allowing you to change the name of the field as used in the pre-defined validation messages.
If you need to be more specific you have two options:
848
+
849
+
- Override a rule’s message using a prop.
850
+
- Override a validation rule’s message globally.
851
+
852
+
### Validation message prop
853
+
854
+
You can easily override validation messages directly on your `FormKit` input by providing an object of strings or functions.
855
+
856
+
#### Using strings
857
+
858
+
To override a validation message on a single FormKit input, add the `validation-messages` prop with an object of rule names and a corresponding message.
| args | An array of arguments passed to the rule. For example <code>['Vue', 'React', 'Angular']</code> from the rule <code>is:Vue,React,Angular</code>. |
877
+
| name | The name of the field (first available from: <code>validation-label</code>, <code>label</code>, then <code>name</code>). |
878
+
| node | The [FormKit core <code>node</code>](/essentials/architecture). |
879
+
880
+
Let’s re-write the above example using a function instead of a string for even more control of the <code>validation-messages</code> prop:
If there are validation rule messages you'd like to override (or add) across your entire project, you can define those message rules when registering FormKit under the language key you'd like to override:
If you would like to render an input’s validation messages outside of the `<FormKit />` component, you can leverage the `<FormKitMessages />` component by passing the input’s node as a prop. Using this component disables the default display of messages (located beneath the input) and moves them to wherever the `<FormKitMessages />` component is located:
FormKit 1.0.0 introduced the [FormKitSummary](/inputs/form#validation-and-error-summary) component which provides an "out of the box" solution to for displaying all the validation messages in a given form or subtree.
933
+
::
934
+
935
+
To get all the validation messages from an [input’s core node](/essentials/architecture), you can use the `getValidationMessages` function exported from `@formkit/validation`. This function will recursively check the given node and all children for validation messages and return a Map of core nodes to validation messages, making it ideal for use with forms:
Trying to manually trigger validation is an anti-pattern in FormKit and should be avoided. Validation is continually computed via fine-grained reactivity. If a value or prop that is used in a validation rule changes, the validation will be automatically be re-run. This ensures validity state, settlement state, submission state and any other validation-related properties can always be trusted.
0 commit comments