How to use Yup validation schema with the Controller component. #3372
Replies: 5 comments 20 replies
-
you have to choose between resolve or build-in validation. if you are using yup, then you probably have to stick with yup reolsver. |
Beta Was this translation helpful? Give feedback.
-
Thank you @bluebill1049
Once again thank you for your help. |
Beta Was this translation helpful? Give feedback.
-
maybe this will help someone else: const schema = yup.object().shape({
email: yup.string().required("is requeried").email("missing email"),
// age: yup.number().positive().integer().required(),
});
import { useFormContext } from "react-hook-form";
export default function FieldEmail() {
const {
register,
formState: { errors },
} = useFormContext();
return (
<>
<input
{...register("email", {
validate: (email) => schema.validate({ email }).catch((e) => e.errors[0]),
})}
/>
<br />
<span>{errors.email && errors.email.message}</span>
</>
);
} |
Beta Was this translation helpful? Give feedback.
-
@bluebill1049 Below is the code reference:
|
Beta Was this translation helpful? Give feedback.
-
@bluebill1049
This is how I needs to structure it before send data
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
SecondaryInput
Controller
component)Parent component
Child component
My code is something like this
NOTE: I can not use the ref inside the custom component as it does not take props like ref
Beta Was this translation helpful? Give feedback.
All reactions