Phoenix
Phoenix

Reputation: 4284

validating deep fields in redux-from

I have wizard redux-from and my fields are something like this:

<Field
    component={TextField}
    className="mui-textfield"
    floatingLabelText="title"
    name="data.title"
    fullWidth={true}
/>

so I want to validate the form with validate function,

Question:

how can I do this work?

Thanks

Upvotes: 3

Views: 58

Answers (1)

Alireza
Alireza

Reputation: 4516

Initially, your error object is empty, so you can't set a nested key, since it's parent is undefined, you have to first define the data key, and then set the error on title inside the data.

You can do something like (Tweak the "!" or any other condition according to your needs)

validate(values)
{
    const errors = {}
    if (!values.data || (values.data && !values.data.title))
        errors.data = {title: "should exist"};
    return errors;
}

Upvotes: 2

Related Questions