bellaratmelia's avatar

custom validation for xlxs and xls

Hi everyone,

I am having issues validating the file input. This is the rule i have in place to validate the file type.

'import'            => 'required|mimes:xls,xlsx',

It works well when the file type is xlsx. But when it is not, instead of returning the error message it populates this error, Exception Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed

I checked the mimes_type.php and both the files exist.

'xls' => 'application/vnd.ms-excel', 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',

Can anyone guide me through on how i can either:

#A) extend validation to create a method to validate the file input to ensure that it is of the right extension/mime type

#B) solve the error that i mentioned on top.

Thanks in advance for the help!

0 likes
2 replies
bellaratmelia's avatar

Hi everyone,

That needs references. After researching i realize that the issue is that i am trying to return the file to the view. So by excluding it, it all works fine now.

return Redirect::to('about')
->withErrors($validator)
->withInput(Input::except('imported_File'))
nolros's avatar

@bellaratmelia you cannot use the mime as you are using it there. Mime is not the extension, but the full string you have listed above. What is tricky is that basic mime types just so happen to have their extension as theri mime type. Think of them as primitives. In the same way mime for pdf won't be pdf but rather the adobe acrobat application string. That said, pdf might be a mime prime.

so you would need to use the full mime name.

'import'            => 'mimes:application/vnd.ms-excel', // assuming that is the right one

if you are not sure you can always pass a file to this to check. I have not tested it, but I think it works.

    /**
     * Get image mime type
     *
     * @param $file
     * @return mixed
     */
    public function getMimeType($file)
    {

        return (finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file));

    }
1 like

Please or to participate in this conversation.