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.
@bluebill1049 I am using you example of custom input (I can't use ref) with minor changes:
- I changed the select to be required and add validate like you suggested required: true, validate: value => { // need to validate if it is undefined or empty array return Array.isArray(value) ? value.length > 0 : !!value; }
- I am using `formState.isValid' to determine the submit button's style
but it doesn't work smoothly - the style changes only after clicking the button
https://codesandbox.io/s/react-hook-form-custom-input-3zk4t
Please note that formState.isValid will only change automatically with onChange and onBlur modes. With the default onSubmit it will only validated on form submission
https://react-hook-form.com/api#formState
Hmm. I tried with onChange but the isValid didn't change. I think there's an issue on how the value of the select is handled.
You'll need to manually run triggerValidation for custom registered fields.
https://codesandbox.io/s/react-hook-form-custom-input-u20bh
Note: If you want the custom registered input to trigger a re-render during its value update, then you should give a type to your registered input.
https://react-hook-form.com/api#register under custom register
thanks @iamdevlinph 🙏 or setValue('name', 'value', true)
Thank you @iamdevlinph and @bluebill1049 ! :-)
thanks @iamdevlinph 🙏 or
setValue('name', 'value', true)
Hey, what exactly does this third param 'true' mean? Is it rerendering or enforcing a valid input?
I was having some trouble removing errors on input and neither setting a type in the register or manually triggering validation was working, but this is :)
@antonbehaeghe check the doc, it's for trigger validation after set value: https://react-hook-form.com/api#setValue
Thank's @iamdevlinph and @bluebill1049 .
Just a quick note : to refresh the component on setValue, it now has to be written this way : setValue('name', 'value', { shouldValidate: true })
@Zykhatslover that's correct, so it will trigger the validation.
@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.
Thank for your example. But the error still appears after choosing value. How to clear error? @bluebill1049 @ethaneisenhard
@hungdev use shouldValidate flag (check the doc)
@hungdev use
shouldValidateflag (check the doc)
Thank for your reply. I used some validation libraries. I really like your lib. and like your enthusiasm for support <3
no worries @hungdev so glad to hear that.