valueAsNumber in Controllers #8068
-
Hi all, I didn't open a bug, since I've read the documentation closely, and it doesn't explicitly say that the controller's rules include valueAsNumber. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 17 replies
-
In the type UseControllerProps<...> = {
...
rules?: Omit<RegisterOptions<TFieldValues, TName>, 'valueAsNumber' | 'valueAsDate' | 'setValueAs' | 'disabled'>;
....
}; You can simply parse your input's value before passing it to the <Controller
- rules={{ valueAsNumber: true }}
render={({ field }) => (
<input
- onChange={field.onChange}
+ onChange={(event) => field.onChange(+event.target.value)}
type="number"
/>
)}
/> |
Beta Was this translation helpful? Give feedback.
-
I think is not good designed and is a not scalable workaround. numerical inputs are numerical, they should be returned as so by react-hook-form. I think is a bad implementation what they did there. Hope they fix it on a future 🙂 |
Beta Was this translation helpful? Give feedback.
-
please add this feature for date and number it will be very helpfull |
Beta Was this translation helpful? Give feedback.
-
I'll just chime in and add my 2 cents. Way back I had a discussion with @bluebill1049 about when to cast form values to actual values. I got the advice to keep all form input as strings and handle the conversion outside of the form, i.e. when setting defaultValues and when submitting. I did as I was adviced, and I'm very happy with this. My inputs behave much better this way than if I had tried to enforce some realtime casting. This is what I do:
Here is how I convert from "componentData", i.e. strings to correct datatypes. Conversion from "typedData" to "componentData":
I found it convenient to separate I'm sorry if my post does not answer the question. I'm just so happy with my solution, so I thought I'd share it. |
Beta Was this translation helpful? Give feedback.
-
I have object
I use but in handleSubmit type of date is always string; |
Beta Was this translation helpful? Give feedback.
Controller
doesn't pack thevalueAs
functionality by design.In the
Controller
types (omitted most of unrelated types for simplicity):You can simply parse your input's value before passing it to the
Controller
'sonChange
handler