react-hook-form icon indicating copy to clipboard operation
react-hook-form copied to clipboard

Custom Input validation

Open alleyshairu opened this issue 7 years ago • 34 comments

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.

Screenshot_2019-05-24_11-55-42

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.

alleyshairu avatar May 24 '19 07:05 alleyshairu

@bluebill1049 I am using you example of custom input (I can't use ref) with minor changes:

  1. 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; }
  2. 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

einatnielsen avatar Feb 11 '20 14:02 einatnielsen

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

iamdevlinph avatar Feb 11 '20 16:02 iamdevlinph

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.

iamdevlinph avatar Feb 11 '20 16:02 iamdevlinph

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

iamdevlinph avatar Feb 11 '20 16:02 iamdevlinph

thanks @iamdevlinph 🙏 or setValue('name', 'value', true)

bluebill1049 avatar Feb 11 '20 20:02 bluebill1049

Thank you @iamdevlinph and @bluebill1049 ! :-)

einatnielsen avatar Feb 12 '20 07:02 einatnielsen

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 avatar Feb 13 '20 14:02 antonbehaeghe

@antonbehaeghe check the doc, it's for trigger validation after set value: https://react-hook-form.com/api#setValue

bluebill1049 avatar Feb 13 '20 21:02 bluebill1049

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 avatar Oct 19 '20 12:10 Zykhatslover

@Zykhatslover that's correct, so it will trigger the validation.

bluebill1049 avatar Oct 19 '20 22:10 bluebill1049

@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 avatar Mar 15 '21 16:03 hungdev

@hungdev use shouldValidate flag (check the doc)

bluebill1049 avatar Mar 15 '21 23:03 bluebill1049

@hungdev use shouldValidate flag (check the doc)

Thank for your reply. I used some validation libraries. I really like your lib. and like your enthusiasm for support <3

hungdev avatar Mar 16 '21 15:03 hungdev

no worries @hungdev so glad to hear that.

bluebill1049 avatar Mar 17 '21 00:03 bluebill1049