Next.js Discord

Discord Forum

How to make the form fields not empty on submit if they are not valid yet?

Answered
Cuvier’s Dwarf Caiman posted this in #help-forum
Open in Discord
Cuvier’s Dwarf CaimanOP
This is my server action:

const submitForm = async (formData: FormData) => {
    'use server'

    const email = formData.get('email') as string
    const password = formData.get('password') as string

    if (email !== '' && password !== '') {
      const signInResult = await signIn({ email, password })
      if (signInResult.ok) {
        redirect('/')
      }
    }
  }


I wonder how to keep the form fields' values present?

Thank you!
Answered by James4u
If you are using Next v15 and React 19, that is expected behavior.
View full answer

16 Replies

Answer
checkout Near's solution
Cuvier’s Dwarf CaimanOP
but I want the fields to keep their values
@Cuvier’s Dwarf Caiman but I want the fields to keep their values
yeah, did you try above code snippet?
Cuvier’s Dwarf CaimanOP
no, as I am not using client components...
it's required, unless as it's the default behavior, form resets when you submit the form
Cuvier’s Dwarf CaimanOP
uh
you must be using next 15?
Cuvier’s Dwarf CaimanOP
yeah i do
then no other options
Cuvier’s Dwarf CaimanOP
sad
downgrade then
Cuvier’s Dwarf CaimanOP
nope
😉
i can handle it