@inertiajs/inertia version: 0.8.2@inertiajs/inertia-vue version: 0.5.5I have a Laravel Jetstream-backed admin interface using Inertia. I'm using the form helper to submit data to the backend. When i do not have any files in my form, it submits just fine, and everything can be stored correctly. As soon as i pick a file, it breaks the data being sent to the backend, so i get an empty request in Laravel.
this.form.patch(
route('$streams.update', {stream: 436),
{
preserveScroll: true
}
)
https://user-images.githubusercontent.com/7515900/107811095-f12c3080-6d6d-11eb-8ee0-d8ea27e7a6b2.mov
Submit a form with a file - observe issue. I'd love trying out whatever to triage the issue, but I've hit a wall here, and I'm starting to doubt whether I have hit a bug, or messed up myself.
Hi,
Most likely this has to do with a long-standing 'bug' in PHP, which doesn't properly accept multipart forms (forms with binary data such as uploads) under PUT/PATCH/DELETE methods. Luckily, however, most frameworks do implement a somewhat 'standard' solution to this that still allows you to use those methods.
Assuming you have something like this now:
this.form = this.$inertia.form({
upload: new File(..) // Your file
});
// ...
this.form.patch(route('$streams.update', {stream: 436}), { preserveScroll: true })
What you'll want to do, is change your request method to POST instead, and add a _method field that contains the _actual_ method that you want to use. This value will then be used on the back-end as the actual incoming HTTP request method:
this.form = this.$inertia.form({
_method: 'patch',
upload: new File(..) // Your file
})
// ...
this.form.post(route('$streams.update', {stream: 436}), { preserveScroll: true })
While I wish we could 'fix' this without any configuration on the user's side, we can't bake this behaviour in by default, as Inertia (while built by and primarily used by Laravel developers) isn't a Laravel-only _or_ PHP-only library, and other languages and frameworks might not have this problem and/or solve it differently.
Hope this helps!
@claudiodekker You were absolutely right - it was the lovely "FormData only works on POST requests in PHP" caveat that got me. Thank you so much for the fast and comprehensive response, I really appreciate it!
Most helpful comment
@claudiodekker You were absolutely right - it was the lovely "FormData only works on POST requests in PHP" caveat that got me. Thank you so much for the fast and comprehensive response, I really appreciate it!