simondavies's avatar

From Validation for File input and Mime types failing...

I not sure why

I have a form file input that I want to use to only upload documents (doc, docx, pdf), so I have set up the validation as below, all other validation works perfectly.

    'document_file' => 'required|mimes:doc,docx,pdf',

But the problem is that it will let docx and doc files through no problem, but any thing else on the list or not like my pdf or even a xlsx file simply reloads the form BUT also without my assigned error message:

    'document_file.mimes' => 'Your document is not the correct format.',

Any ideas that i am missing or need to check for this. Are the setting I need to add, could it be a server side issue etc.

NB: I am using the validation class outside of laravel as well.

thanks in advance

0 likes
5 replies
simondavies's avatar

@Mbezdek i have tried that and just did again incase, but nothing, its like its failing silently, thanks

in fact when i try to investigate and dd() the returned validation variable i get the following along with the messages, might help or not.

      Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed' in [no active file]:0 Stack trace: #0 {main} thrown in [no active file] on line 0

then also checking out the validation messages the actual feedback about the uploaded file comes back ok but it looks like the above error is whats throwing it off... In fact it appears for submitting any file upload, if left blank the error disappears, seems to be the a field upload issue...

thomaskim's avatar

@simondavies I just tried your code, and it worked fine on my end.

How are you displaying the error messages? Can you also post your entire request class?

It's also possible that the size of your PDF exceeds the default file size limit / POST data size of your server.

simondavies's avatar

@thomaskim thanks for the reply, I will look at the pdf files size, but its a test one for now with one line of text. have left for now will be back on it when i get home so might be able to post then. As mentioned i'm using some laravel stuff outside of the frame work, but am using below with in my code.

    use Illuminate\Http\Request;

class myClassName {
        function __construct(..){
             $this->request = Request::createFromGlobals();
             ....
      }
     
      public function validateForm(){
            $validate = $this->validation->validate($this->request->all());
           .....
      }

  }

then in my validation class

    $validate = $this->factory->make($data, self::Rules(), self::Messages());
   if ($validate->fails()) {
        return self::ReadableErrors($validate->messages());
  }
  ....

as mentioned for ref that symfony error happens when sending $this->request->all() but then does not report error when i send through $this->request->except('document_file'), but then I cannot validate that normally.

So am doing it via the file ext and then simply adding the error if there is one to the message stack like so $validate->errors()->add('document','Sorry wrong file type');

Please or to participate in this conversation.