Search code examples
laravelvalidationfile-uploadinertiajs

Laravel Inertia Vue Precognition File Upload


i am trying to upload a pdf file through a form. I am using Laravel Inertia Vue Stack and Laravel precognition. these are my FormRequestValidation

return [
  'type' => 'required|in:Antidadah,Bas',
  'aim' => 'required|string',
  'target_visitor' => 'required|integer',
  'location' => 'required|string',
  'kumpulan' => 'required|string',
  'ref_no' => 'required|string',
  'status_id' => 'required|exists:statuses,code',
  'file' => [
     ...$this->isPrecognitive() ? [] : ['required'],
     'file',
     'mimes:pdf',
]

this is my form in vue

const form = useForm(
    props.request ? "patch" : "post",
    props.request
        ? route("requests.update", props.request)
        : route("requests.store"),
    {
        type: props.request ? props.request.type : null,
        aim: props.request ? props.request.aim : null,
        target_visitor: props.request
            ? props.request.target_visitor
            : null,
        location: props.request ? props.request.location : null,
        kumpulan: props.request ? props.request.kumpulan : null,
        ref_no: props.request ? props.request.ref_no : null,
        status_id: props.request ? props.request.status_id : null,
        user: props.request ? props.request.user : null,
        note: props.request ? props.request.note : null,
        file: null,
    },
);

When I submit the form without a file, all works well, but when I submit the form with a file, all other field will be invalidated. Can anyone point me what is happening?


Solution

  • I am having the same issue with Laravel Precognition and Vue.

    As Matteo mentioned, in Laravel you need to include @method('PUT') inside the form tags in order to use the PUT method in a form. I did this, and like you Rasyidi, when I select a file to upload, the precognition response invalidates all the other fields.

    When I changed the route and @method to POST, there are no issues. This is a bit of a hack though, and I'd rather use the proper method of PUT.