Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 742a2e7

Browse files
committed
Updating docs
1 parent 63313d6 commit 742a2e7

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

docs/guides/faqs.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ const BirthDate = ({forModel}) => (
5656
export default BirthDate;
5757
```
5858

59+
Also, see [./validation.md](the validation guide) for more information.
60+
5961
### How do I get the component instance? `ref={...}` doesn't work.
6062

6163
Use `getRef={(node) => ...}` in place of `ref`. This is due to the fact that React treats the `ref` prop as a "magic" prop that doesn't get propagated down through wrapped components.

docs/guides/validation.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,73 @@ Validation occurs as the result of dispatching validation actions, such as `acti
66
- validate any key on that model (such as `{ required: true, length: false }`)
77
- call validation only when the model has been updated.
88

9+
## Quick Reference
10+
11+
**Component Validation**
12+
```jsx
13+
const isEmail = (val) => /* check if val is email */
14+
15+
// HTML5 validation
16+
// Works with any HTML5 constraint validation attributes
17+
<Control.text type="email" model="user.email" required />
18+
19+
// Keyed validation
20+
<Control.text
21+
model="user.email"
22+
validators={{
23+
required: (val) => val && val.length,
24+
isEmail, // ES6 property shorthand
25+
}}
26+
/>
27+
28+
// Keyed errors
29+
<Control.text
30+
model="user.email"
31+
errors={{
32+
required: (val) => !val || !val.length,
33+
isEmail: (val) => !isEmail(val),
34+
}}
35+
/>
36+
37+
// Form-level validation
38+
const longEnough = (val) => val && val.length > 8;
39+
40+
<Form
41+
model="user"
42+
validators={{
43+
'': {
44+
// Form-level validator
45+
passwordsMatch: (vals) => vals.password === vals.confirmPassword,
46+
},
47+
// Field-level validators
48+
password: { longEnough },
49+
confirmPassword: { longEnough },
50+
}}
51+
/>
52+
```
53+
54+
**Actions**
55+
```jsx
56+
// Set validity of entire model
57+
dispatch(actions.setValidity('foo.bar', true));
58+
59+
// Set keyed validity of entire model
60+
dispatch(actions.setValidity('foo.bar', {
61+
isLongEnough: true,
62+
isRightFormat: false,
63+
}));
64+
65+
// Set errors of entire model.
66+
// Truthy values indicate an error.
67+
dispatch(actions.setErrors('foo.bar', true));
68+
69+
// Set keyed validity of entire model
70+
dispatch(actions.setErrors('foo.bar', {
71+
isLongEnough: false, // not an error
72+
isRightFormat: true, // an error
73+
}));
74+
```
75+
976
## Simple Validation
1077

1178
Suppose you are validating `'user.email'`. At any point, you can dispatch `actions.setValidity()` to set the validity of that model on your form state:
@@ -235,6 +302,28 @@ import { Form } from 'react-redux-form';
235302
</Form>
236303
```
237304
305+
## Validating across models
306+
307+
Any validation across models is best represented as a form-level validator. For instance, say you have a form where the two password fields, `.password` and `.confirmPassword`, need to match:
308+
309+
```jsx
310+
<Form
311+
model="user"
312+
validators={{
313+
'': {
314+
passwordsMatch: (vals) => vals.password === vals.confirmPassword,
315+
},
316+
}}
317+
>
318+
<Control type="password" model=".password" />
319+
<Control type="password" model=".confirmPassword" />
320+
321+
<Errors model="user" />
322+
</Form>
323+
```
324+
325+
When any of the `user` model values change, the form-level validity will be updated. You can manually retrieve form-level validation by accessing `[form path].$form.validity`, which represents the validity of the entire form.
326+
238327
## Deep Model Validation in `<Form>`
239328
240329
As of RRF version 1.2.4, you can have deep validators in the `<Form validators={{...}}>` prop. Here's what it looks like:

0 commit comments

Comments
 (0)