Dave More
Dave More

Reputation: 11

How to validate the weight of the image in Backpack for laravel?

I am working with backpackforlaravel ( "backpack/crud": "3.5.*"), it is great. I made an image CRUD section. I have a Requests file validation with the following code.

return [
            'name' => 'required|min:5|max:255',
            'code' => 'required|min:2|max:20',
            'image' => 'required|mimes:jpeg,png,jpg,gif|max:2048',
        ];

And in my controller I have this code

$this->crud->addField([ // image
            'label' => "Bandera",
            'name' => "image",
            'type' => 'image',
            'upload' => true,
            'crop' => false, // set to true to allow cropping, false to disable
            'aspect_ratio' => 1, // ommit or set to 0 to allow any aspect ratio
            'mime_types' => ['image'],
            'filesize'      => 5,
            // 'disk' => 's3_bucket', // in case you need to show images from a different disk
            // 'prefix' => 'uploads/images/profile_pictures/' // in case your db value is only the file name (no path), you can use this to prepend your path to the image src (in HTML), before it's shown to the user;
        ]);

Right now it is showing The image must be a file of type: jpeg, png, jpg, gif. The image may not be greater than 2048 characters.

It is validation the image weigh as a string and I need a normal weight and type validation

Upvotes: 1

Views: 1180

Answers (1)

JorisJ1
JorisJ1

Reputation: 989

You are validating an image correctly when using that array of validators.

  • Validating mimes:jpeg,png,jpg,gif checks if it is an image.
  • Validating max:2048 puts a maximum on the filesize.

Upvotes: 1

Related Questions