use:enhance form and validation errors - should field values be returned #10197
-
Therefore, in a progressively enhanced Question is, assuming that the <form method="POST" use:enhance>
<!-- On a validation error, this input is not reset. Value property is not necessary. -->
<input name="email" type="email" value={form?.email ?? ''}>
</form> The validation errors documentation illustrates that certain field values can be returned in a form action. import { fail } from '@sveltejs/kit';
if (!user || user.password !== hash(password)) {
return fail(400, { email, incorrect: true });
}
<input name="email" type="email" value={form?.email ?? ''}> |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Just an User Experience thing. This is totally up to the design. Do you want your users to re-type everything if the action fails? If you don't know, go to other sites and see how they handle login form (enter random credentials and observe the error handling). |
Beta Was this translation helpful? Give feedback.
This works as expected. You can disable form reset like so
If you want to persist the state when javascript isnt active, you need to return the form values in your action alongside any errors https://kit.svelte.dev/docs/form-actions#anatomy-of-an-action-validation-errors