Custom error message for Laravel validation rule: Dimensions

◇◆丶佛笑我妖孽 提交于 2019-12-22 08:14:02

问题


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:


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'
    ]
 ]


来源:https://stackoverflow.com/questions/47335447/custom-error-message-for-laravel-validation-rule-dimensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!