Skip to content

Commit d68c0b0

Browse files
committed
Further advanced validation rule example
1 parent 05b631b commit d68c0b0

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

13/umbraco-forms/editor/creating-a-form/form-advanced.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ To add new rules, you need to provide the rule definition, an error message and
2222

2323
Crafting the rule definition itself requires use of [JSON logic](https://jsonlogic.com/) along with placeholders for the field or fields that are being validated.
2424

25+
### Examples
26+
2527
One example use case would be ensuring that two fields match each other, perhaps when asking for a user's email address. Given two fields on the form, one with the alias of `email` and the other `compareEmail`, the rule would be:
2628

2729
```json
@@ -60,6 +62,37 @@ A slightly more complex example could be with two dates, where, if provided, you
6062
}
6163
```
6264

65+
Rules can be nested too. In this final illustrative example, we have two fields. One with the alias `choose` is a drop-down list with two values: `A` and `B`. The second field with alias `test` we want to be completed only if the user selects `B`. So we craft a rule that is valid only if A is selected OR B is selected AND `test` is completed.
66+
67+
```json
68+
{
69+
"or": [
70+
{
71+
"==": [
72+
"{choose}",
73+
"A"
74+
]
75+
},
76+
{
77+
"and": [
78+
{
79+
"==": [
80+
"{choose}",
81+
"B"
82+
]
83+
},
84+
{
85+
"!=": [
86+
"{test}",
87+
""
88+
]
89+
}
90+
]
91+
}
92+
]
93+
}
94+
```
95+
6396
Overall you should be able to create simple or more complex rules as needed, using comparisons between fields and with static values.
6497

6598
When the form is rendered, these validation rules will be applied both client and server-side. In this way you can ensure the submission is only accepted if it meets the requirements.

0 commit comments

Comments
 (0)