Next.js Discord

Discord Forum

Can I clear input on revalidate?

Unanswered
Thrianta posted this in #help-forum
Open in Discord
ThriantaOP
I'm trying to make this but when I'm revalidating my path my form is not clearing. without making CSR component is there any way to do it

I have provided my code in comment.

and I'm looking for a solution to give error toast if value is empty but in ssr it's not possible to use toast is there any way to do that without making this page csr

6 Replies

ThriantaOP
my code is
<CardBody>
                        <form className="flex flex-col gap-2" action={onSubmit}>
                            <Input type="text" variant="underlined" placeholder="Computer Science Engineering" label="Department Name" color="danger" name="dpt_name" isRequired />
                            <Input type="text" variant="underlined" placeholder="CSE" label="Department Name in Short" color="danger" name="dpt_shortName" isRequired />
                            <SubmitButton text="Add Department" className="w-full" />
                        </form>
                    </CardBody>
and my onSubmit function is
const onSubmit = async (formData: FormData) => {
        "use server"
        const dpt_name = formData.get("dpt_name") as string;
        const dpt_shortName = formData.get("dpt_shortName") as string;
        if (!dpt_name || !dpt_shortName) {
            return;
        } else {
            const res = await db.department.create({
                data: {
                    dpt_name, dpt_shortName
                }
            })
            if (res) {
                revalidatePath("/admin_dashboard/departments")
            }
        }

    }
I posted about this a few days ago. Have a look at the example https://sabin.dev/how-to-clear-your-forms-when-using-server-actions-in-nextjs
@sabin I posted about this a few days ago. Have a look at the example https://sabin.dev/how-to-clear-your-forms-when-using-server-actions-in-nextjs
ThriantaOP
sir that means i have to make my form client component in order to clear form
Yes, do you see any issues with that?