<Form method="post"> <input name="email" type="email" required /> <button type="submit">Submit</button> </Form>
The required attribute is handled by the browser's built-in validation. It prevents the form from submitting if the field is empty, showing a message to the user. This happens before Remix or the server sees the data.
import { json, redirect } from '@remix-run/node'; import { Form, useActionData } from '@remix-run/react'; export async function action({ request }) { const formData = await request.formData(); const username = formData.get('username'); if (!username || username.length < 3) { return json({ errors: { username: 'Username too short' } }, { status: 400 }); } return redirect('/success'); } export default function FormComponent() { const actionData = useActionData(); return ( <Form method="post"> <input name="username" /> {actionData?.errors?.username && <p>{actionData.errors.username}</p>} <button type="submit">Submit</button> </Form> ); }
The action function returns a JSON object with an errors property containing the validation message. The component reads this via useActionData() and displays the error.
Option C safely accesses the errors property from the object returned by useActionData(). Option C treats the whole object as errors, B tries to destructure from undefined if useActionData returns null, and D is a syntax error.
import { json, redirect } from '@remix-run/node'; export async function action({ request }) { const formData = await request.formData(); const email = formData.get('email'); if (!email.includes('@')) { return json({ errors: { email: 'Invalid email' } }, { status: 400 }); } return redirect('/success'); }
Remix requires action functions to return a Response object or use the json() helper to send JSON data. Returning a plain object does not send data to the client properly, so errors never appear.
Remix leverages the browser's built-in HTML validation to catch simple errors before submission. Then the action function performs server-side validation to ensure data integrity and security. This layered approach improves user experience and safety.