Reputation: 43
I tried to follow along with a zod validation tutorial that is in svelte. I have no experience with this (svelte) so I'm wonder how do I replicate this codes functionality inside of a simple react/next app? I'm stuck on what ({request}) would be/what I'd be passing to the async function.
Additionally, should this be going inside of a useEffect or just called upon 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 repo for the tutorial: https://github.com/huntabyte/sveltekit-form-validation/blob/main/src/routes/%2Bpage.server.js
Upvotes: 0
Views: 1333
Reputation: 26
Here's what I did:
Overall, I found that this approach worked well and was easy to understand and implement. As for your question about where to put the validation code, I would recommend calling it on form submission rather than inside useEffect.
Upvotes: 1