Is the below considered a valid use of redux-form Fields?
const ValidatedFieldGroup = (props) => {
const {meta: {touched, error}} = props
return (
<div className={touched && error ? 'has-error' : ''}>
<Field name="one" .... />
<Field name="two" .... />
</div>
)
}
const MyMainComponent = (props) => {
return <Field name="twofields"
component={ValidatedFieldGroup}
validate={myValidator} .... />
}
const myValidator = (value, allValues) => {
return (
allValues.one === "pretend-this-is-valid" && allValues.two === "pretend-this-is-valid"
) ? undefined : 'One of the fields is invalid - sort it out!'
}
A 'valueless' parent Field is implemented solely to hook into the sync field level validation pipeline. The props of this component can then be used to alter the UI / state of it's children (which in turn contain some RF Fields implementing the actual form values)
Real-world example = assume there are a group of five check boxes... if at least TWO of them are not checked then they should ALL be wrapped in a 'red bordered' div.
In seems to work thus far, but I am conscious that there may be an easier / better / correct way to achieve the same result or that I could in fact be setting myself up for future troubles!!
Thanks in advance.
Although this work-around results in the desired UI (namely, a single
divwith the correctclass), you'll end up with three fields in the redux-form store,one,two, andtwofields, which seems undesirable. Presumably, you'll never do anything with the fieldtwofieldson the backend, since it's only used for presentation. This goes against the idea that theredux-formstore should map to the fields in your backend (DB, or whatever...).You could instead use the Fields component, so that you only register the
oneandtwofields, which is more consistent:Then put your validator against
redux-formin the config: