React Hook Form: Validation scenarios When Integrating with Custom Input Components
I'm currently using React Hook Form (v7.22.0) and have created a custom input component for an address form. However, I'm working with an scenario where the validation schema using `Yup` seems to unexpected result when I try to submit the form. The custom input doesn't seem to trigger the validation checks as expected. Hereβs the relevant code for the custom input: ```javascript import React from 'react'; import { useFormContext } from 'react-hook-form'; const CustomInput = ({ name, label }) => { const { register, formState: { errors } } = useFormContext(); return ( <div> <label>{label}</label> <input {...register(name)} /> {errors[name] && <span>{errors[name].message}</span>} </div> ); }; export default CustomInput; ``` And here's how I'm using it in the main form component: ```javascript import React from 'react'; import { useForm, FormProvider } from 'react-hook-form'; import * as Yup from 'yup'; import { yupResolver } from '@hookform/resolvers/yup'; import CustomInput from './CustomInput'; const schema = Yup.object().shape({ street: Yup.string().required('Street is required'), city: Yup.string().required('City is required'), }); const AddressForm = () => { const methods = useForm({ resolver: yupResolver(schema) }); const onSubmit = data => { console.log(data); }; return ( <FormProvider {...methods}> <form onSubmit={methods.handleSubmit(onSubmit)}> <CustomInput name="street" label="Street" /> <CustomInput name="city" label="City" /> <button type="submit">Submit</button> </form> </FormProvider> ); }; export default AddressForm; ``` When I fill in the fields and try to submit, I see the following behavior message in the console: "Validation failed: street is required" even though I have entered a value. It seems like the `register` function isn't correctly binding to my custom input. I've tried moving the `register` call into the `input` component directly, but it didnβt solve the scenario. I also checked for any typos in the names used in both `CustomInput` and the validation schema. What could I be missing here that prevents the validation from working as intended?