Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Multiple Validator Wrong Error Message

Problem : When uploaded file exceeds 5000kb, validator returns the 'required' message instead of 'max' message. Why ?

$file = (Input::file('inputName'));
$fileValidator = Validator::make(
   array('Field Name' => $file),
   array('Field Name' => 'required|max:5000|mimes:jpeg,png,bmp')
);

if($fileValidator->fails()){
   return $fileValidator->errors()->all(':message');
}

Update : This problem occurs especially *.psd files' validation.


Update 2 : when i var_dump($file), i can see that;

object(Symfony\Component\HttpFoundation\File\UploadedFile)#9 (7) {
  ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
  bool(false)
  ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
  string(52) "4-47970_rsasecurityanalyticsevolutionofsiemebook.pdf"
  ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
  string(24) "application/octet-stream"
  ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
  int(0)
  ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
  int(1)
  ["pathName":"SplFileInfo":private]=>
  string(0) ""
  ["fileName":"SplFileInfo":private]=>
  string(0) ""
}

As you can see, the pathName and fileName looks null. So that's why laravel returns required message. Here's the new question : why is the fileName is null ?

like image 605
saimcan Avatar asked Sep 27 '22 02:09

saimcan


1 Answers

When you upload a file more than the allowed size (max post size and max upload size), then php does not send it to the server, that is why your code does not get file and through an error of required.

go to your php.ini and increase the limit of max upload and max post size. this should solve your issue.

you can also set these by php:

ini_set('post_max_size', '64M');
ini_set('upload_max_filesize', '64M');

or you can edit your php.ini file for these:

post_max_size = 64M
upload_max_filesize = 64M
like image 107
Anshul Mishra Avatar answered Oct 05 '22 01:10

Anshul Mishra