Search code examples
laravellaravel-5laravel-validation

laravel validator not validating image image dimensions correct


I am trying to validate the image width and height. it is giving error even if user select image of correct resolution.

controller code

$messages = [
    'productImages.dimensions' => "Profile image must be maximum 100x100 "
];
$this->validate($request,[
    'productName'           => 'required|max:40',
    'productDescription'    => 'required|max:1000',
    'productCondition'      => 'required',
    'productImages'         =>     'required|max:5|dimensions:max_width=100,max_height=100',
    'category'              => 'required'
],$messages);

I want to submit that image when user select image of correct resolution.


Solution

  • Based on the fact productImages is plural and you have set a max value, I'm guessing that you are passing an array of images, not a single image. As such, you would need to validate the dimensions for each item of the array separately.

    $this->validate($request,[
        'productName'           => 'required|max:40',
        'productDescription'    => 'required|max:1000',
        'productCondition'      => 'required',
        'productImages'         => 'required|max:5',
        'productImages.*'       => 'dimensions:max_width=100,max_height=100',
        'category'              => 'required'
    ],$messages);