Custom Input validation
Hello, I can't seem to figure out why the validation doesn't work on custom inputs like Select2.
I have created a fork of your custom input example, which already registers the select input, so I just added the required validation, now I expect that the reactSelect value will be available when the onSubmit function is triggered, but it's missing from data.

My understanding is that required validation will make sure all inputs values are available when the onSubmit function is triggered. Maybe I am wrong? I also can't seem to make other validation rules work for custom input.
How would you go about solving this? setError maybe?
and Thank you for creating this.
Hey @AlleyShairu,
Is this what you after?
for a required field, you need to do register({ required: true })
let me know if this makes sense?
cheers bill
looks like react-select return empty array when nothing is selected, I may need to patch to fix required validation. will keep you updated.
@bluebill1049 Take the below component as an example mentioned in the codepen
<Select
className="reactSelect"
name="filters"
placeholder="Filters"
value={values.selectedOption}
options={options}
onChange={handleMultiChange}
ref={e => register({ name: "reactSelect", required: true })}
/>
I have added the register({ name: "reactSelect", required: true })
Now what would happen if I submit the form without interacting with this input?
- If it's submitting the form I should see the
reactSelectvalue in the data object, even if it's the empty array. - Or it shouldn't let me submit the form and populates the error object.
This is what I am getting after clicking the submit button without interacting with the input.

I am getting the same behavior with other components like react datepicker.
Thanks for spending your time on this.
Thanks for the issue report, I will sort out ASAP.
https://codesandbox.io/s/react-hook-form-custom-input-dyh9q
try this one, let me know if make sense.
https://github.com/bluebill1049/react-hook-form/releases/tag/v3.7.3
It does, tested with react-datepicker too.
So now to make the validation work on custom input, we should use the second parameter for rules like (required or validate) in register function.
Thanks a lot for your hard work.
yea :) that's correct. I used to validate with react select because return value can be an empty array. If you find the package useful please kindly leave a star to support, much appreciated. ❤️
@alleyshairu could you share that example with the react-date-picker?? that would be awesome
@ethaneisenhard https://codesandbox.io/s/stoic-kirch-6oq7p I just created this for you.
Basically, maintain the state of your custom input like you usually do, but also register the input so that the react-form-hook can know about it. You usually do it like
React.useEffect(() => {
register({ name: "date" }, { required: true });
}, []);
and then whenever the input value change you use the react-form-hook setValue to assign the value to the input you registered. Now, this value will be available onSubmit.
<DatePicker
isClearable
selected={date}
onChange={val => {
setDate(val);
setValue("date", val); // Here we are setting the value for the registered input.
}}
/>
Hope this was helpful.
thanks a lot @alleyshairu for sharing with us!
For some reason when doing the custom register with <FormContext /> the methods.handleSubmit does not fire.
The parent component which contains the form
const Page: React.FunctionComponent = () => {
const onSubmit = () => {
console.log("clicked");
methods.handleSubmit(data => {
console.log("data", data); // <-- this does not log
});
};
return (
<FormContext {...methods}>
<form className="w-full">
<FormSectionWithDate></FormSectionWithDate>
<div className="py-6">
<button onClick={onSubmit}>Save</button>
</div>
</form>
</FormContext>
);
};
The date component with the custom register
const DatePicker: React.FunctionComponent = () => {
const [startDate, setStartDate] = useState<any>(new Date());
useEffect(() => {
register({ name: "date" }, { required: true });
}, []);
return (
<DatePicker
onChange={val => {
setStartDate(val);
setValue(name, val);
console.log(val);
}}
value={startDate}
/>
);
};
This is the the date picker library I'm using is wojtekmaj/react-date-picker
Also tried with [register] but still no luck
useEffect(() => {
register({ name: "date" }, { required: true });
}, [register]);
@iamdevlinph can you share a codesandbox for this and which version of RHF are you using?
I'm trying to create one but I always get a cross-origin error when I add the following code.
<form onSubmit={methods.handleSubmit(onSubmit)}>
I might just create a repository for it
I just realized the issue.
I was doing the wrong object for register
useEffect(() => {
register({ name }, { required });
}, []);
instead of
useEffect(() => {
register({ name, required });
}, []);
🤦♂️🤦♂️
@iamdevlinph all good. show me the error that you got if you have sometime too :)
I see. I understood my issue now.
With this syntax
useEffect(() => {
register({ name, required });
}, []);
the required is treated as a form field that's why the methods.handleSubmit triggers even though there are empty required fields.
While in this syntax (which is correct)
useEffect(() => {
register({ name}, {required});
}, []);
the required is the actual required option and that's why when I submit the form it doesn't trigger anything because there are empty required fields.
This is just a user error so I'm not sure if this can be prevented in the library. Maybe add a warning or prompt when a required is detected in the Element obj like,
"It seems you are using 'required' as an input name or something"
all good @iamdevlinph thanks for let us know too 👍
next major version, we are simplify the API a bit. hopefully make your guys' life easier too.
It's quite simple enough. Just some bad eyesight 😅
@iamdevlinph if you find it useful in the future, Please consider leave us a star too 🙏