I tried to follow the zod verification tutorial in svelte. I have no experience with this (svelte) so I'm wondering how can I replicate this code functionality in a simple React/next app? I'm stuck on what ({request}) will be/will be passed to the async function.
Also, should this be done inside useEffect or only called on form submission?
export const actions = { default: async ({ request }) => { const formData = Object.fromEntries(await request.formData()); console.log('Form Data:', formData); try { const result = registerSchema.parse(formData); console.log('SUCCESS'); console.log(result); } catch (err) { const { fieldErrors: errors } = err.flatten(); const { password, passwordConfirm, ...rest } = formData; return { data: rest, errors }; } };
Here is the repository for this tutorial: https://github.com/huntabyte/sveltekit-form-validation/blob/main/src/routes/+page.server.js
This is what I did:
Overall, I find this approach works well and is easy to understand and implement. As for your question about where to put the validation code, I would suggest calling it on form submission rather than calling it in useEffect.