phplaravel-9

Laravel Image Upload Validation Using Postman


So I am trying to validate the uploaded images in my laravel project, however the File::image() validaton invalidates my input even though I am uploading a file. I am using postman to do this.

Here is my validatio rules.

'product_name' => 'required|string|unique:' . Product::class,
'product_stock_amount' => 'required|integer|numeric|gte:0',
'product_price' => 'required|integer|numeric|gte:0',
'product_price_currency' => 'required|string|'. Rule::in(config('lappee.accepted_currency')),
'product_description' => 'nullable|string',
'product_images' => [
 'nullable',
 File::image()->max(12 * 1024)->dimensions(Rule::dimensions()->maxWidth(config('lappee.allowed_image_size.width'))->maxHeight(config('lappee.allowed_image_size.height')))
],

Here's my postman.

enter image description here

I am I doing someething wrong?


Solution

  • Your validation expects that product_images is only one file, but you are sending product_images as an array. So you have to update your validation like this:

    'product_name' => 'required|string|unique:' . Product::class,
    'product_stock_amount' => 'required|integer|numeric|gte:0',
    'product_price' => 'required|integer|numeric|gte:0',
    'product_price_currency' => 'required|string|'. Rule::in(config('lappee.accepted_currency')),
    'product_description' => 'nullable|string',
    'product_images.*' => [
     'nullable',
     File::image()->max(12 * 1024)->dimensions(Rule::dimensions()->maxWidth(config('lappee.allowed_image_size.width'))->maxHeight(config('lappee.allowed_image_size.height')))
    ],