Custom error message for Laravel validation rule:

2019-05-08 19:14发布

I'm trying to validate an image upload that looks like the following:

$this->validate($request, [
    'admin_image'=> 'nullable|image|dimensions:min_width=600,min_height=600',
]);

when the selected image too small then laravel shows error:

The Admin Image has invalid image dimensiona

I think that message is not saying specifically that in which dimension the image small, eg: width or height.

I'm expecting error message like: The Admin Image width cannot be less than 600px and The Admin Image height cannot be less than 600px

here 'Admin image' is the attribute name & '600' is the value I given in rules

So, I wanted to make a custom error message for min_width and max_widthin custom messages array on validation.php, that look like the following:

'admin_image' => [
            'dimensions.min_width' => 'The :attribute dimension (width) cannot be less than :min_width px',
            'dimensions.min_height' => 'The :attribute dimension (height) cannot be less than :min_height px',
        ],

But unfortunately that doesn't work & Laravel continues to show the default message.

Please understand that

I need 2 separate error message for dimensions:min_width=600 & dimensions:min_height=600

like I tried in the custom error messages array.

I know this is very simple but I'm doing something wrong.

Any help will be highly appreciated

1条回答
Summer. ? 凉城
2楼-- · 2019-05-08 20:13

Add the custom error message in validation.php as below:

'admin_image' => [
    'dimensions' => [
        'min_width' => 'The :attribute dimension (width) cannot be less than :min_width px'
    ]
 ]
查看更多
登录 后发表回答