Confused about handling form submissions with Server Actions in Next.js 15 #85721
-
        Summaryhello there I'm exploring the new Server Actions feature in Next.js 15, and I'm a bit confused about how to handle form submissions properly. For example, I have a simple contact form: It works fine locally, but I'm wondering — 1- Is this approach still recommended for production apps? 2- Should I handle validation inside the action or client-side first? 3- What’s the best way to return a success/error message back to the user? Any examples or advice would be super helpful 🙏 Thanks in advance! Additional information``jsx
<form action={sendMessage}>
  <input type="text" name="name" />
  <input type="email" name="email" />
  <textarea name="message" />
  <button type="submit">Send</button>
</form>
and my server action looks like this:
'use server'
export async function sendMessage(formData) {
  const name = formData.get('name')
  console.log('New message from:', name)
}ExampleNo response  | 
  
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
        
 Almost, prod ready, but you got it right, you should do validation and a tad more. Client side validation is to help your users submit the data that's most likely to succeed their operation, reducing friction when you require certain formats/types of data. Infamously, emails, passwords, identity numbers, and a few other fields often have strict format/shape requirements, you wouldn't want your user to submit the form only to then, tell them that a field was incorrectly formatted. Server side validation ought to always be in place, and it is not just for type safety, but correctness toward your resources. You definitely don't want malformed data, incorrect, or pass through from users, reaching into your database or forwarded to other services. You need to verify each field, each value, take only those your operation require (filter), and enforce your business rules on the incoming values. 3- What’s the best way to return a success/error message back to the user? I'd say,   | 
  
Beta Was this translation helpful? Give feedback.
-
        
 You can reference this repo for better understanding: QuestionForm.tsx  | 
  
Beta Was this translation helpful? Give feedback.
Almost, prod ready, but you got it right, you should do validation and a tad more. Client side validation is to help your users submit the data that's most likely to succeed their operation, reducing friction when you require certain formats/types of data. Infamously, emails, passwords, identity numbers, and a few other fields often have strict format/shape requirements, you wouldn't want your user to submit the form only to then, tell them that a field was incorrectly formatted.
Server side validation ought to always be in place, and it is not just for type s…