Yup validation schema does not work with nested objects
I modified the yup schema validation example to use nested objects. However validations are not working for nested objects.
Please see my CodeSandbox
I know there was a related issue (https://github.com/react-hook-form/react-hook-form/issues/893) in January, however I am not sure if it was fixed.
I am using the latest versions except for @hookform/resolvers. Looks like v1.00 has some API changes, so reverted back to v0.0.3 which was used in the original example.
My project uses nested objects very heavily, so it is important to get this working. Please help!
Hi @nareshbhatia !
Your schema validation works well ! :)
Your schema is:
const SignupSchema = yup.object().shape({
person: yup.object().shape({
firstName: yup.string().required(),
age: yup.number().required().positive().integer(),
website: yup.string().url()
})
});
So to display errors you should do:
<div>
<label>First Name</label>
<input type="text" name="person.firstName" ref={register} />
{errors.person?.firstName && <p>{errors.person?.firstName.message}</p>}
</div>
instead of:
<div>
<label>First Name</label>
<input type="text" name="person.firstName" ref={register} />
{errors.firstName && <p>{errors.firstName.message}</p>}
</div>
You have forgotten person => errors.person?.firstName
➡️ https://codesandbox.io/s/react-hook-form-v6-validationschema-forked-q7tlg
Wow, how could I miss that? Embarrassed :-). Thank you so much @jorisre, you made my day!
thank you @jorisre 🙏 ❤️
Hi @nareshbhatia !
Your schema validation works well ! :)
Your schema is:
const SignupSchema = yup.object().shape({ person: yup.object().shape({ firstName: yup.string().required(), age: yup.number().required().positive().integer(), website: yup.string().url() }) });So to display errors you should do:
<div> <label>First Name</label> <input type="text" name="person.firstName" ref={register} /> {errors.person?.firstName && <p>{errors.person?.firstName.message}</p>} </div>instead of:
<div> <label>First Name</label> <input type="text" name="person.firstName" ref={register} /> {errors.firstName && <p>{errors.firstName.message}</p>} </div>You have forgotten
person=>errors.person?.firstName➡️ https://codesandbox.io/s/react-hook-form-v6-validationschema-forked-q7tlg
I have been dealing with same problem. Thank you!
Hi. Had same issue and was resolved, but to only one level deep of object. I have...
const NewPackSchema = yup.object().shape({
reference: yup.string().required("Reference is a required field"),
createdById: yup.string().required("Created by is required"),
requestDetail: yup.object().shape({
reference: yup.string().required("REQ Reference is a required field"),
applicantDetail: yup.object().shape({
fullName: yup.string().required("Fullname is a required field"),
}),
}),
});
and get error
TypeError: Cannot read properties of undefined (reading 'message')
on ...
<TextField
label="Fullname"
{...register("requestDetail.applicantDetail.fullName")}
error={
errors.requestDetail?.applicantDetail?.fullName ? true : false}
/>
... help would be appreciated, thanks.
@Biz-e-Services Hello, I find similar difficulties. I found a solution using get method of lodash library, This function gets the value at path of object first import get method from lodash
import { get } from 'lodash'
then ,
<TextField label="Fullname" {...register("requestDetail.applicantDetail.fullName")}
error={ get(errors, '"requestDetail.applicantDetail.fullName") && error['message'] } />
clean code:
let fullname = "requestDetail.applicantDetail.fullName"
<TextField label="Fullname" {...register(fullname)} error={ get(errors, fullname) && error['message'] } />