Skip to main content
Glama
brianirish

Laravel 12 Docs MCP Server

by brianirish
forms.md6.68 kB
# Inertia - Forms Source: https://inertiajs.com/forms # Forms ## Submitting forms ## Server-side validation ## Form helper ### Wayfinder ## File uploads ## XHR / fetch submissions While it's possible to make classic HTML form submissions with Inertia, it's not recommended since they cause full-page reloads. Instead, it's better to intercept form submissions and then make the request using Inertia. As you may have noticed in the example above, when using Inertia, you don't typically need to inspect form responses client-side like you would when making XHR / fetch requests manually. Instead, your server-side route / controller typically issues a redirect response. And, Of course, there is nothing stopping you from redirecting the user right back to the page they were previously on. Using this approach, handling Inertia form submissions feels very similar to handling classic HTML form submissions. Handling server-side validation errors in Inertia works a little different than handling errors from manual XHR / fetch requests. When making XHR / fetch requests, you typically inspect the response for a 422 status code and manually update the form's error state. However, when using Inertia, a 422 response is never returned by your server. Instead, as we saw in the example above, your routes / controllers will typically return a redirect response - much like a classic, full-page form submission. For a full discussion on handling and displaying validation errors with Inertia, please consult the validation documentation. Since working with forms is so common, Inertia includes a form helper designed to help reduce the amount of boilerplate code needed for handling typical form submissions. To submit the form, you may use the get, post, put, patch and delete methods. The submit methods support all of the typical visit options, such as preserveState, preserveScroll, and event callbacks, which can be helpful for performing tasks on successful form submissions. For example, you might use the onSuccess callback to reset inputs to their original state. If you need to modify the form data before it's sent to the server, you can do so via the transform() method. You can use the processing property to track if a form is currently being submitted. This can be helpful for preventing double form submissions by disabling the submit button. If your form is uploading files, the current progress event is available via the progress property, allowing you to easily display the upload progress. To determine if a form has any errors, you may use the hasErrors property. To clear form errors, use the clearErrors() method. If you're using client-side input validation libraries or do client-side validation manually, you can set your own errors on the form using the setErrors() method. When a form has been successfully submitted, the wasSuccessful property will be true. In addition to this, forms have a recentlySuccessful property, which will be set to true for two seconds after a successful form submission. This property can be utilized to show temporary success messages. To reset the form's values back to their default values, you can use the reset() method. Sometimes, you may want to restore your form fields to their default values and clear any validation errors at the same time. Instead of calling reset() and clearErrors() separately, you can use the resetAndClearErrors() method, which combines both actions into a single call. If your form's default values become outdated, you can use the defaults() method to update them. Then, the form will be reset to the correct values the next time the reset() method is invoked. To determine if a form has any changes, you may use the isDirty property. To cancel a form submission, use the cancel() method. To instruct Inertia to store a form's data and errors in history state, you can provide a unique form key as the first argument when instantiating your form. When using Wayfinder in conjunction with the form helper, you can simply pass the resulting object directly to the form.submit method. The form helper will infer the HTTP method and URL from the Wayfinder object: When making requests or form submissions that include files, Inertia will automatically convert the request data into a FormData object. For a more thorough discussion of file uploads, please consult the file uploads documentation. Using Inertia to submit forms works great for the vast majority of situations; however, in the event that you need more control over the form submission, you're free to make plain XHR or fetch requests instead using the library of your choice. If there are form validation errors, they are available via the errors property. When building Laravel powered Inertia applications, form errors will automatically be populated when your application throws instances of ValidationException, such as when using . `422` `get` `post` `put` `patch` `delete` `preserveState` `preserveScroll` `onSuccess` `transform()` `processing` `progress` `errors` `ValidationException` `hasErrors` `clearErrors()` `setErrors()` `wasSuccessful` `true` `recentlySuccessful` `reset()` `resetAndClearErrors()` `defaults()` `isDirty` `cancel()` `form.submit` `FormData` `fetch` [request using Inertia](/manual-visits) [redirect](/redirects) [validation](/validation) [visit options](/manual-visits) [validation documentation](/validation) [history state](/remembering-state) [Wayfinder](https://github.com/laravel/wayfinder) [file uploads documentation](/file-uploads) s not recommended since they cause full-page reloads. Instead, it // Clear all errors... form.clearErrors() // Clear errors for specific fields... form.clearErrors( // Clear all errors... $form.clearErrors() // Clear errors for specific fields... $form.clearErrors( // Set a single error... form.setError( Some other error for the bar field. // Set a single error $form.setError( // Reset the form... form.reset() // Reset specific fields... form.reset( // Reset the form... $form.reset() // Reset specific fields... $form.reset( // Reset the form and clear all errors... form.resetAndClearErrors() // Reset specific fields and clear their errors... form.resetAndClearErrors( // Reset the form and clear all errors... $form.resetAndClearErrors() // Reset specific fields and clear their errors... $form.resetAndClearErrors(

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/brianirish/laravel-docs-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server