other-emerald
other-emerald16mo ago

remove spaces before submit

Hi everyone !! I try to modify a phone number to remove space before i send to the API. I get from a mui phone input. I have a state in which i store the phone number. When i submit i have a value like this +33 6 10 20 30 40. I have made a function to remove the spaces like following :
const removeSpaces = (phrase: string) => {
return phrase.replace(/\s/g, "");
};
const removeSpaces = (phrase: string) => {
return phrase.replace(/\s/g, "");
};
which i call in the on finish like this :
const handleSubmitUsersEdit = (values: FieldValues) => {
values.mobile_phone_number = removeSpaces(values.mobile_phone_number)
onFinish({...values});
};
const handleSubmitUsersEdit = (values: FieldValues) => {
values.mobile_phone_number = removeSpaces(values.mobile_phone_number)
onFinish({...values});
};
i also tried this :
const handleSubmitUsersEdit = (values: FieldValues) => {
onFinish({...values, mobile_phone_number: removeSpaces(values.mobile_phone_number)});
};
const handleSubmitUsersEdit = (values: FieldValues) => {
onFinish({...values, mobile_phone_number: removeSpaces(values.mobile_phone_number)});
};
but i always have the same issue which is : edit.tsx:90 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'split') or replace in the other case. My question is : How edit the phone number to remove the spaces before i submit it to the API ?
2 Replies
refine-support-bot
Hi, I am refine support bot :wave:. I am here to help you with your question. I searched for the answer to your question in the refine documentation and found an answer! 🏆
Answer: You can modify the form data before submitting it to the API by using the transformValues option in the useForm hook. This option allows you to transform the form values before they are sent to the API. SOURCES: ./docs/api-reference/mantine/hooks/form/useForm.md, ./docs/faq.md

Note: If this is the answer you need, then we are very lucky. If not, please wait for someone from the refine core team or the community to try to help you.
stormy-gold
stormy-gold16mo ago
You can do it the way you write. But you get an error while editing the phone number. I suggest you check it out.