pronextjs / forms-management Goto Github PK
View Code? Open in Web Editor NEWForm validation and posting example in the NextJS App Router
Form validation and posting example in the NextJS App Router
Hey @jherr , thank you for the great tutorial about form management. I learned a ton!
I was reviewing the code attached to the lesson Implement a Server Action for Handling Form Data
and I noticed that in Github the onSubmit
on the <form .../>
is different to what I see on the video. Maybe a typo?
What I see on your video:
But what I have on Github is:
https://github.com/ProNextJS/forms-management/blob/main/src/app/RegistrationForm.tsx#L95C6-L100C11
Issue Title:
Error: Form Submit
Description:
After adding the useFormState
hook and switching to manual form submission using formRef
, I encountered issues with the functionality: time to time, I get the error (attached screenshot)
Expected Behavior:
We can workaround it using client and react-hook-form in such way:
const form = useForm<FormValues>({
resolver: zodResolver(schema),
defaultValues: {
email: '',
password: '',
confirmEmail: '',
},
});
const {
formState: { errors },
} = form;
const onSubmit = async (data: FormValues) => {
const response = await signUpAction(data);
if (response.error) {
form.setError('root.serverError', {
type: '400',
message: response.message,
});
}
};
// and view (JSX)
{errors.root?.['serverError'] && (
<HelperText className="my-4 text-center" variant="error">
<b>Server Error: </b>
{errors.root?.['serverError'].message}
</HelperText>
)}
But your version is elegant and much better.
Actual Behavior:
Screenshot with error attached.
It happens here
<form
action={formAction}
className="flex flex-col gap-y-28"
onSubmit={form.handleSubmit(() => formRef?.current?.submit())} // <=== Here.
ref={formRef}
>
Link to place in repo https://github.com/ProNextJS/forms-management/blob/main/src/app/RegistrationForm.tsx#L95C8-L95C71
Recommendations in Error stack trace also don't work :(
Have you come across such issue?
Additional Information:
Operating System: macOS Sonoma 14.4.1
Browser/Software Version: Google Chrome Version 123.0.6312.87
Thanks for putting this together! I’ve got my whole team working through the course this week 💪
When taking the course myself, I noticed a small typo in the lesson text of the “Implement Async Server-Side Validation” chapter:
In the following code block,
"use server";
export async function validateZipCode(zipCode: string): Promise<boolean> {
console.log("validateZipcode on SERVER", zipCode)
return /^/d{5}.test(zipCode) && zipCode.startsWith('9');
}
/^/d{5}.test(zipCode)
should be /^\d{5}/.test(zipCode)
instead. The original code causes a syntax error crash when used.
Also, validateZipcode.ts
is duplicated as validateZipcode.tsx
in the repository. There should be only be the .ts
one, right?
Thanks again! We’re excited to put this pattern into development ✨
Thanks so much for the great tutorial.
Would it be possible to show an example of an input of type="file"?
My attempt give the following error:
Type '{ onChange: (...event: any[]) => void; onBlur: Noop; value: File; disabled?: boolean | undefined; name: "file"; ref: RefCallBack; placeholder: string; accept: string; type: "file"; }' is not assignable to type 'InputProps'. Types of property 'value' are incompatible. Type 'File' is not assignable to type 'string | number | readonly string[] | undefined'.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.