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
{{ message }}
This repository was archived by the owner on Aug 23, 2022. It is now read-only.
It can also be a function that returns a string model. See [the documentation on tracking](../guides/tracking.md) for more information.
92
93
93
94
<h2id="prop-mapProps"></h2>
94
-
## `mapProps={\{...}}`
95
+
## `mapProps={{...}}`
95
96
_(Object | Function)_: A custom mapping from props provided by `Control` to props received by the component. Can be:
96
97
97
98
- An object, where each value is a function from original props to corresponding value in result props.
@@ -100,7 +101,7 @@ _(Object | Function)_: A custom mapping from props provided by `Control` to prop
100
101
Examples:
101
102
```jsx
102
103
<Control
103
-
mapProps={\{
104
+
mapProps={{
104
105
customChange: (props) =>props.change,
105
106
}}
106
107
model="..."
@@ -129,15 +130,15 @@ You can also specify `updateOn={['change', 'blur']}` as an array of one or more
129
130
- Use the `changeAction` prop if you want to dispatch custom actions along with the `actions.change(...)` action.
130
131
131
132
<h2id="prop-validators"></h2>
132
-
## `validators={\{...}}`
133
+
## `validators={{...}}`
133
134
_(Object)_: A map where the keys are validation keys, and the values are the corresponding functions that determine the validity of each key, given the model's value.
134
135
135
136
For example, this control validates that a username exists and is longer than 4 characters:
@@ -169,7 +170,7 @@ _(String | Array)_: A string/array of strings specifying when validation should
169
170
- To avoid displaying error messages on load (as controls might be invalid), use the `.pristine` property of the control when conditionally showing error messages, or use the `<Errors>` component.
170
171
171
172
<h2id="prop-asyncValidators"></h2>
172
-
## `asyncValidators={\{...}}`
173
+
## `asyncValidators={{...}}`
173
174
_(Object)_: A map where the keys are validation keys, and the values are the corresponding functions that (asynchronously) determine the validity of each key, given the model's value.
174
175
175
176
Each async validator function is called with 2 arguments:
@@ -183,7 +184,7 @@ For example, this control validates that a username is available via a promise:
183
184
importisAvailablefrom'../path/to/is-available';
184
185
185
186
<Control.text model="user.username"
186
-
asyncValidators={\{
187
+
asyncValidators={{
187
188
isAvailable: (value, done) => {
188
189
isAvailable(value)
189
190
.then((result) =>done(result));
@@ -202,7 +203,7 @@ _(String | Array)_: A string/array of strings specifying when async validation s
202
203
-`"focus"` - validate on the `onFocus` event handler
203
204
204
205
<h2id="prop-errors"></h2>
205
-
## `errors={\{...}}`
206
+
## `errors={{...}}`
206
207
_(Object)_: A map where the keys are error keys, and the values are the corresponding error validator functions that determine the invalidity of each key, given the model's value.
207
208
208
209
An **error validator** is a function that returns `true` or a truthy value (such as a string) if invalid, and `false` if valid.
@@ -212,7 +213,7 @@ For example, this control validates that a username exists and is longer than 4
212
213
```jsx
213
214
<Control.text
214
215
model="user.username"
215
-
errors={\{
216
+
errors={{
216
217
isEmpty: (val) =>!val.length,
217
218
tooLong: (val) =>val.length>16,
218
219
}}
@@ -281,7 +282,7 @@ function changeAndSubmit(model, value) {
281
282
- Since `changeAction` expects an action creator and `redux-thunk` is used, you can asynchronously dispatch actions (like the example above).
282
283
283
284
<h2id="prop-controlProps"></h2>
284
-
## `controlProps={\{...}}`
285
+
## `controlProps={{...}}`
285
286
_(Object)_: A mapping of control-specific props that will be applied directly to the rendered control. In some cases, this can be a safer way of applying props, especially if there are naming conflicts between `<Control>`-specific props (such as `"model"`) and props that need to go on the rendered control (e.g., `<input {...props} />`).
286
287
287
288
The normal behavior is that any extraneous props on `<Control>` that are not part of `Control.propTypes` (which are documented here) will be given to the rendered input.
@@ -293,7 +294,7 @@ Example:
293
294
<Control.text
294
295
model="..."
295
296
component={CustomInput}
296
-
controlProps={\{errors:'errors for CustomInput'}}
297
+
controlProps={{errors:'errors for CustomInput'}}
297
298
/>
298
299
```
299
300
@@ -336,7 +337,7 @@ However, in `<Control>`, it can be a boolean, or a function, string, or object a
336
337
// Disable the submit button when the form is invalid
337
338
<Control.button
338
339
model="user"
339
-
disabled={\{ valid:false }}
340
+
disabled={{ valid:false }}
340
341
>
341
342
Submit!
342
343
</Control.button>
@@ -345,7 +346,7 @@ However, in `<Control>`, it can be a boolean, or a function, string, or object a
345
346
For example:
346
347
-`disabled={true}` or `disabled={false}` will disable or enable the control respectively, as will any other primitive value, such as `undefined`, `null`, or a number
347
348
-`disabled="touched"` will disable if the field is `touched` (works with any property on the field)
348
-
-`disabled={\{ valid: false, touched: true }}` will disable if the field is both `touched` and not `valid`
349
+
-`disabled={{ valid: false, touched: true }}` will disable if the field is both `touched` and not `valid`
349
350
-`disabled={(fieldValue) => !fieldValue.valid}` will call the function provided with the `fieldValue` to determine its `disabled` state.
350
351
351
352
(since: version 1.3.0)
@@ -371,7 +372,7 @@ _(Boolean)_: Signifies that the field state (validation, etc.) should not persis
Copy file name to clipboardExpand all lines: docs/api/Errors.md
+9-7Lines changed: 9 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,4 @@
1
+
{% raw %}
1
2
# Errors Component
2
3
3
4
## `<Errors />`
@@ -9,12 +10,12 @@ The `<Errors />` component provides a handy way of displaying form errors for a
9
10
<Control.text
10
11
type="email"
11
12
model="user.email"
12
-
validators={\{ isEmail, isRequired }}
13
+
validators={{ isEmail, isRequired }}
13
14
/>
14
15
15
16
<Errors
16
17
model="user.email"
17
-
messages={\{
18
+
messages={{
18
19
isRequired:'Please provide an email address.',
19
20
isEmail: (val) =>`${val} is not a valid email.`,
20
21
}}
@@ -48,13 +49,13 @@ _(String | Function)_: the string representation of the model path to show the e
48
49
```jsx
49
50
<Errors
50
51
model="user"
51
-
messages={\{
52
+
messages={{
52
53
passwordsMatch:'Passwords do not match.',
53
54
}}
54
55
/>
55
56
```
56
57
57
-
## `messages={\{...}}`
58
+
## `messages={{...}}`
58
59
59
60
_(Object)_: a plain object mapping where:
60
61
- the keys are error keys (such as `"required"`)
@@ -67,7 +68,7 @@ If the message value is a function, it will be called with the model value.
67
68
```jsx
68
69
<Errors
69
70
model="user.email"
70
-
messages={\{
71
+
messages={{
71
72
required:'Please enter an email address.',
72
73
length:'The email address is too long.',
73
74
invalid: (val) =>`${val} is not a valid email address.',
@@ -78,7 +79,7 @@ If the message value is a function, it will be called with the model value.
78
79
### Notes
79
80
- The `messages` prop is a great place to keep custom error messages that can vary based on the location in the UI, instead of hardcoding error messages in validation fuctions.
80
81
- If a message is _not_ provided for an error key, the message will default to the key value in the control's `.errors` property.
81
-
- This means if you're using `actions.setErrors` or the `errors={\{...}}` prop in`<Control>` or `<Field>` to set error messages, they will automatically be shown in`<Errors />`.
82
+
- This means if you're using `actions.setErrors` or the `errors={{...}}` prop in`<Control>` or `<Field>` to set error messages, they will automatically be shown in`<Errors />`.
82
83
83
84
## `show={...}`
84
85
@@ -90,7 +91,7 @@ It can be a boolean, or a function, string, or object as a [Lodash iteratee](htt
90
91
### Examples
91
92
- `show={true}` will always show the errors if they exist
92
93
- `show={(field) => field.touched && !field.focus}` will show errors if the model's field is touched and not focused
93
-
-`show={\{touched: true, focus: false}}` is the same as above
94
+
-`show={{touched: true, focus: false}}` is the same as above
94
95
-`show="touched"` will show errors if the model's field is touched
95
96
96
97
### Notes
@@ -119,3 +120,4 @@ _(String | Function | Element)_: The `component`, which is the component for eac
119
120
- `fieldValue` - the current field state of the `model`
120
121
- `children` - the error message (text).
121
122
- `component={CustomError}` will wrap the error in a `<CustomError>` component, which will receive the same props as above.
You can also use [partial models](../guides/partial-models.md) for `<Control>`, `<Field>`, `<Fieldset>`, and `<Errors>` components inside of `<Form>` - they will be resolved to the form's model.
51
51
52
-
## `validators={\{...}}`
52
+
## `validators={{...}}`
53
53
_(Object)_: An object representing the validators for the fields inside the form, where:
54
54
55
55
- the **keys** are the field model names (e.g. `'email'` for `user.email`)
@@ -65,15 +65,15 @@ Validation will occur on _any field model change_ by default, and only the valid
65
65
- Specifying validators on the form is usually sufficient - you don't need to put validators on the `<Field>` or `<Control>` for most use cases.
66
66
- If you need validators to run on submit, this is the place to put them.
67
67
68
-
## `errors={\{...}}`
68
+
## `errors={{...}}`
69
69
_(Object)_: An object representing the error validators for the fields inside the form, where:
70
70
71
71
- the **keys** are the field model names (e.g. `'email'` for `user.email`)
72
72
- the **values** are error validator(s) for the field model. They can be:
73
73
- an error validator function, which receives the field model value, or
74
74
- an error validator object, with validation keys and error validator functions as values, also receiving the field model value.
75
75
76
-
Its behavior is identical to the `validators={\{...}}` prop, with the exception that an error validator that returns anything truthy is interpreted as an _error_. See [the validation guide](../guides/validation.md) for more info.
76
+
Its behavior is identical to the `validators={{...}}` prop, with the exception that an error validator that returns anything truthy is interpreted as an _error_. See [the validation guide](../guides/validation.md) for more info.
77
77
78
78
## `validateOn="..."`
79
79
_(String)_: A string that indicates when `validators` or `errors` (for error validators) should run.
@@ -126,7 +126,7 @@ class MyForm extends React.Component {
126
126
return (
127
127
<Form
128
128
model="user"
129
-
validators={\{...}}
129
+
validators={{...}}
130
130
onSubmit={ (user) =>this.handleSubmit(user) }
131
131
>
132
132
<Control type="email" model=".email"/>
@@ -170,7 +170,7 @@ class MyForm extends React.Component {
Some custom controls won't have prop keys that match up exactly with the standard event handler props, such as `onChangeText` in React Native's `<TextInput>` component corresponding to `onChange`. You can specify a prop mapping in the `mapProps={\{...}}` prop to specify the mapping:
142
+
Some custom controls won't have prop keys that match up exactly with the standard event handler props, such as `onChangeText` in React Native's `<TextInput>` component corresponding to `onChange`. You can specify a prop mapping in the `mapProps={{...}}` prop to specify the mapping:
0 commit comments