You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello everyone, i'm working on a project where i have to send data to a server action. Since i'm using formData in the server action all the data i get is a string so when i use zod the validation doesn't work. Is there any clean way to do it, so i can convert without doing it in the action for each field which is not clean :)
`export async function addCoin(prevState: FormState, data: FormData): Promise {
// await new Promise(resolve => setTimeout(resolve, 3000));
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Hello everyone, i'm working on a project where i have to send data to a server action. Since i'm using formData in the server action all the data i get is a string so when i use zod the validation doesn't work. Is there any clean way to do it, so i can convert without doing it in the action for each field which is not clean :)
`export async function addCoin(prevState: FormState, data: FormData): Promise {
// await new Promise(resolve => setTimeout(resolve, 3000));
const formData = Object.fromEntries(data);
const parsed = addCoinSchema.safeParse(formData);
if (!parsed.success) {
const fields = Object.fromEntries(
parsed.error.issues.map(issue => {
const key = issue.path[0];
const message = issue.message;
return [key, String(message ?? '')];
})
);
return {
message: 'Invalid form',
fields,
};
}
// call backend for processing
return { message: 'Form submitted successfuly' };
}`
Non-Goals
Background
Actually i'm struggling to find a solution.
Proposal
More documentation :)
Beta Was this translation helpful? Give feedback.
All reactions