DavidSprauel's avatar

Laravel validation odd behavior

I'm working on a file upload and it upload well until I try to get a file I'm not supposed to be able to upload.

My rules are:

 public function rules()
    {
        return [
            'media' => ['required', 'image', 'max:2000']
        ];
    }

public function messages()
    {
        return [
            'media.required' => 'You must give a file to upload!',
            'media.image'    => 'The file is not an image!',
            'media.max'      => 'The file is too big!',
        ];
    }

and when I try to upload a file which is 2,3Mo I got a 422 but the message is always The given data is invalid without telling me which one is invalid.

Then in my controller, this is how I use it:

public function uploadMedia(AddMediaRequest $request, MyEntity $entity)
    {
        $filename = $entity->addMedia($request->validated());

        return response()->json(['filename' => $filename], 200);
    }

Am I missing a simple point ? (I use Vue for the front end with axios)

0 likes
6 replies
UsmanBasharmal's avatar

Are you sure that it's Laravel and not your webserver/PHP configuration that stops you?

UsmanBasharmal's avatar

You set 'max:2000' in your rules which is less than 2 Megabytes and you are trying to upload a file which size is 2.3 Megabytes so I think that is the error.

DavidSprauel's avatar

Sure, I know about that but I don't get a proper error message, I get just a generic one : The given data is invalid instead of The file is too big!

DavidSprauel's avatar

I run several website on this server and got no problem with others :/

UsmanBasharmal's avatar

But did you investigate? Did you check your server configuration? The default PHP configuration has a 2MB max for file uploads. This means that your file will never reach your Laravel application and it will be your PHP implementation (or your webserver) that rejects the request

DavidSprauel's avatar

Yes I did, in php.ini, this is on 8MB and in nginx config, 128M.

I receive a 422 and a laravel generic error message.

Please or to participate in this conversation.