This repository has been archived by the owner on Aug 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 251
How to validate nested/deep models/values #510
Labels
Comments
Currently, you can only do something like this: const required = (val) => !!val;
<Form model="user"
validators={{
phones: { required: (numbers) => numbers.every(required) }
}}
>
// ... etc. The above is not exactly a workaround, just a way to tell the user that they forgot to fill out one of the many phone numbers they have. That's why I'm marking this as an enhancement; ideally you should be able to do this: const required = (val) => !!val;
<Form model="user"
validators={{
'phones[].number': { required }
}}
>
// ... etc. For a real workaround, you can always attach |
I found another workaround: <Form model="user"
validators={{
'phones.0.number': { required }
}}
> A more automated aproch: render() {
const required = (val) => !!val;
const { phones } = this.props.user;
const phoneNumbersValidators = phones.reduce(
(validators, _, index) => {
validators[`phones.${index}.number`] = { required };
return validators;
}, {});
return (
<Form model="user"
validators={phoneNumbersValidators)}
>
Thanks |
@davidkpiano Thanks, working 'almost' perfect, when the form is submited it becomes invalid, added some tests on this branch: https://github.com/tiagoefmoraes/react-redux-form/tree/deep_validation_submit I fixed, will open a PR soon |
tiagoefmoraes
added a commit
to tiagoefmoraes/react-redux-form
that referenced
this issue
Nov 17, 2016
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I need to validate deep values of a model
user.phones[].number
.Tried setting Form validators to
"phones.number": { required: (val) => !!val }
"phones[].number": { required: (val) => !!val }
phones: { number: { required: (val) => !!val }}
but none worked.
And a form for the user:
Working example: https://esnextb.in/?gist=5cb62ba0efa95ce18bd82709c33be6bf
Any way of doing this?
The text was updated successfully, but these errors were encountered: