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
+57-222
Original file line number
Diff line number
Diff line change
@@ -167,7 +167,7 @@ You can use rule hints together. To do so, just place multiple hints before the
167
167
168
168
## Available rules
169
169
170
-
FormKit ships with over 20 production-ready validation rules, covering most validation needs. If you don’t find one that meets your exact requirement, you can add a [custom rule](#custom-rules) to suit your needs.
170
+
FormKit ships with over 35 production-ready validation rules, covering most validation needs. If you don't find one that meets your exact requirement, you can add a [custom rule](#custom-rules) to suit your needs.
171
171
172
172
-[accepted](#accepted)
173
173
-[alpha](#alpha)
@@ -185,6 +185,10 @@ FormKit ships with over 20 production-ready validation rules, covering most vali
185
185
-[date_after](#date-after)
186
186
-[date_before](#date-before)
187
187
-[date_between](#date-between)
188
+
-[date_before_or_equal](#date-before-or-equal)
189
+
-[date_after_or_equal](#date-after-or-equal)
190
+
-[date_before_node](#date-before-node)
191
+
-[date_after_node](#date-after-node)
188
192
-[date_format](#date-format)
189
193
-[email](#email)
190
194
-[ends_with](#ends-with)
@@ -265,7 +269,7 @@ layout: "auto"
265
269
266
270
### Confirm
267
271
268
-
Checks if the value of one input matches the value of another input— often used for password confirmations. There are two ways to specify which input to match:
272
+
Checks if the value of one input matches the value of another input— often used for password confirmations. There are two ways to specify which input to match:
269
273
270
274
- Append `_confirm` to the `name` attribute of the second input.
271
275
- Pass the `name` of the first input as an argument to the confirm rule in the second input `confirm:name_of_input_1` (more specific).
@@ -400,9 +404,57 @@ layout: "auto"
400
404
---
401
405
::
402
406
407
+
### Date before or equal
408
+
409
+
Determines if a date is before or equal to the current date or a date supplied as the rule's argument. Dates used can either be JavaScript `Date` objects or strings that can be parsed by [`Date.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse).
Determines if a date is after or equal to the current date or a date supplied as the rule's argument. Dates used can either be JavaScript `Date` objects or strings that can be parsed by [`Date.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse).
Determines if a date is before the date in another node specified by its address. The address is provided as an argument to the rule. Dates used can either be JavaScript `Date` objects or strings that can be parsed by [`Date.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse).
Determines if a date is after the date in another node specified by its address. The address is provided as an argument to the rule. Dates used can either be JavaScript `Date` objects or strings that can be parsed by [`Date.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse).
@@ -418,7 +470,7 @@ Ensures the format of an input’s date matches a specific date format. The form
418
470
type: "warning"
419
471
label: ""
420
472
---
421
-
Native date inputs always output the same format <code>YYYY-MM-DD ...</code> even though they display dates according to the browser’s locale. Using this rule to specify a <em>different</em> format would result in an input that can never be valid.
473
+
Native date inputs always output the same format <code>YYYY-MM-DD ...</code> even though they display dates according to the browser's locale. Using this rule to specify a <em>different</em> format would result in an input that can never be valid.
422
474
::
423
475
424
476
::Example
@@ -467,7 +519,7 @@ layout: "auto"
467
519
468
520
### Length
469
521
470
-
Checks that the input’s value is over a given length, or between two length values. It works to validate arrays (like [lists](/inputs/list)), objects (like [groups](/inputs/group)), or string lengths. Can be used to simulate the native `maxlength` and `minlength` as well.
522
+
Checks that the input's value is over a given length, or between two length values. It works to validate arrays (like [lists](/inputs/list)), objects (like [groups](/inputs/group)), or string lengths. Can be used to simulate the native `maxlength` and `minlength` as well.
471
523
472
524
::Example
473
525
---
@@ -676,220 +728,3 @@ layout: "auto"
676
728
## Custom rules
677
729
678
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:
679
-
680
-
```js
681
-
/**
682
-
* File: my-custom-rules/monday.js
683
-
*
684
-
* A contrived validation rule that ensures the input’s value is monday or mon.
685
-
*/
686
-
constmonday=function (node) {
687
-
returnnode.value==='monday'||node.value==='mon'
688
-
}
689
-
690
-
exportdefaultmonday
691
-
```
692
-
693
-
### Defining custom rule behaviors
694
-
695
-
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:
696
-
697
-
```js
698
-
/**
699
-
* A contrived validation rule that ensures the input’s value is monday or mon.
700
-
*/
701
-
constmonday=function (node) {
702
-
returnnode.value==='monday'||node.value==='mon'
703
-
}
704
-
705
-
// override default rule behaviors for your custom rule
706
-
monday.blocking=false
707
-
monday.skipEmpty=false
708
-
monday.debounce=20// milliseconds
709
-
monday.force=true
710
-
711
-
exportdefaultmonday
712
-
```
713
-
714
-
You can also override these behaviors on a case-by-case basis with [rule hints](#rule-hints).
715
-
716
-
Once you have a validation function written — you need to register the validation rule with FormKit — either globally or specifically on an input.
717
-
718
-
### Multi-input validation rules
719
-
720
-
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.
781
-
::
782
-
783
-
## Custom messages
784
-
785
-
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:
796
-
797
-
- Override a rule’s message using a prop.
798
-
- Override a validation rule’s message globally.
799
-
800
-
### Validation message prop
801
-
802
-
You can easily override validation messages directly on your `FormKit` input by providing an object of strings or functions.
803
-
804
-
#### Using strings
805
-
806
-
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>. |
825
-
| name | The name of the field (first available from: <code>validation-label</code>, <code>label</code>, then <code>name</code>). |
826
-
| node | The [FormKit core <code>node</code>](/essentials/architecture). |
827
-
828
-
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.
881
-
::
882
-
883
-
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