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
Cuvier’s Dwarf CaimanOP
This is my server action:
I wonder how to keep the form fields' values present?
Thank you!
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.
16 Replies
@Cuvier’s Dwarf Caiman This is my server action:
ts
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!
If you are using Next v15 and React 19, that is expected behavior.
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