Typecast form values before validation #10627
-
I have a list of numeric values, something like What should I do in order to tell react-hook-form that I want to treat the values of some fields in a different way than strings? I tried a combination of What seems to work, is to typecast in the resolver: const methods = useForm<MyForm>({
resolver: (data: MyForm, context, options) => {
data.categories = data.categories.map(id => Number(id));
data.typeId = Number(data.typeId);
const resolver = classValidatorResolver(MyForm, {
validator: {
groups: [`step:${formState.currentStepNumber}`],
stopAtFirstError: true
},
});
return resolver(data, context, options);
},
reValidateMode: 'onSubmit',
defaultValues: formState,
}); but I don't know if this is okay or there's a better way. Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
We had similar issue with one component where custom select would always return the selected value as string even though our form had number. Fixed it by converting the value back to number before calling |
Beta Was this translation helpful? Give feedback.
Sure. In our case the
SomeDropdown
only accepts strings so we just convert theselectedItem
to string and back tonumber
like so: