$app/forms module provides utilities for enhancing HTML forms with client-side interactivity while maintaining functionality without JavaScript.
enhance
Theenhance action progressively enhances a <form> element that otherwise would work without JavaScript.
Enhances a form element with progressive enhancement capabilities.Parameters:
form_element(HTMLFormElement) - The form element to enhancesubmit(SubmitFunction) - Optional callback function invoked upon submission
destroy method for cleanupBasic Usage
Custom Submit Handler
Thesubmit function is called upon submission with the given FormData and the action that should be triggered. If cancel is called, the form will not be submitted.
Submit Function Parameters
The URL to which the form is being submitted
The FormData object containing form values
Reference to the form element
Call this function to prevent form submission
AbortController to cancel the submission if another one starts
The element that triggered the submission (button or input)
Callback Function Parameters
The response from the server action
Invokes the default behavior. Accepts options:
reset: false- Don’t reset form values after successful submissioninvalidateAll: false- Don’t callinvalidateAllafter submission
deserialize
Use this function to deserialize the response from a form submission.Deserializes form action responses.Parameters:
result(string) - The serialized response text
applyAction
Applies an action result to the current page state.Applies the given action result to the page.Parameters:
result(ActionResult) - The action result to apply
Default Behavior
If you don’t provide a custom submit function or callback, the default behavior is:- Updates the
formprop with returned data if the action is on the same page - Updates
page.status - Resets the form and invalidates all data on successful submission with no redirect
- Redirects on redirect response
- Navigates to nearest error page on unexpected error
Important Notes
Forms with file inputs must include
enctype="multipart/form-data" attribute.