Next.js Discord

Discord Forum

React Hook Form with Currency Field that is saved as integer (pennies)

Unanswered
Northeast Congo Lion posted this in #help-forum
Open in Discord
Northeast Congo LionOP
Can anyone point me at an example or explain how you collect data from a form that includes a money field. I have tried input with type number with a step as any (or .01). I have tried to use setValueAs to multiple it by 100 to get rid of decimals. I've tried as string. Nothing seems to work as it keeps rounding the number before setValueAs takes place.

This seems to be such a common thing, yet I cannot find any example. All examples seem to use Age or some other whole number as their topic. but nothing with decimals or money.

<input type="number" step="any" 
{...register('amount', { setValueAs: v => (parseInt(v)*100) } )}
/>

tried a string variation of this as well.

In the schema setup, its using zod and the amount field has been either z.number() or z.coerce.number() or z.string()

What is the proper way?

0 Replies