so I’m creating a multi-step-form using shadcn
, react-hook-form
and nextjs
. How can I disable the next button if the user didn’t complete the first part of the form?
what I did here is, I created a two button that will increment and decrement.
const [currentFormPage, setCurrentFormPage] = useState(1);
const goToNextPage = () => {
if (currentFormPage !== 3) {
setCurrentFormPage(currentFormPage + 1);
}
};
const goToPreviewsPage = () => {
if (currentFormPage != 1) {
setCurrentFormPage(currentFormPage - 1);
}
};
The next thing I did here is I created a logic that will change the form according to the currentPage.
{
currentFormPage === 1 && (
<DialogHeader>
<DialogTitle>General Information</DialogTitle>
</DialogHeader>
<FormField
control={form.control}
name="title"
render={({ field }) => (
<FormItem>
<FormLabel>Title</FormLabel>
<FormControl>
<Input placeholder="title of event" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input placeholder="email" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="fullName"
render={({ field }) => (
<FormItem>
<FormLabel>Full Name</FormLabel>
<FormControl>
<Input placeholder="Full name" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
);
}
{
currentFormPage === 2 && (
<DialogHeader>
<DialogTitle>Additional Information</DialogTitle>
</DialogHeader>
);
}
{
currentFormPage === 3 && (
<DialogHeader>
<DialogTitle>Additional Information</DialogTitle>
</DialogHeader>
);
}
<DialogFooter>
<Button variant="outline" onClick={goToPreviewsPage}>
Back
</Button>
<Button variant="outline" onClick={goToNextPage}>
Next
</Button>
</DialogFooter>;
Now the problem I’m encountering here is, I can still press the next button, even though I didn’t complete the first form.
2
Answers
You can detect that using states. put each of the forms of different pages into states and based on that you can disable or enable the next button.
declaring state to hold the form values of each page:
then declare a function to handle each field changes:
create another function to get all the fields of the form of the current page:
Another function to check each of the form fields of current page and update the type of the next button:
now, your form field comps will look like this:
Finally, the next button will be:
To disable the next button until the user completes the first part of the form, you can add a condition to check if the required fields in the first part are filled before allowing the user to proceed to the next step. You can achieve this by utilizing the state to track the completion status of the first part of the form. Here’s how you can do it:
In this code:
A state variable isFirstPartCompleted is added to track whether the required fields in the first part of the form are filled.
An onSubmitFirstPage function is included to execute when the user submits the first part of the form. Within this function, validation occurs to ensure the required fields are filled. If validation passes, isFirstPartCompleted is set to true.
Within the goToNextPage function, a condition is added to verify if isFirstPartCompleted is true before permitting the user to proceed to the next step. If isFirstPartCompleted is false, the user cannot proceed until the first part is completed.