简体   繁体   English

Laravel 验证规则的自定义错误消息:维度

[英]Custom error message for Laravel validation rule: Dimensions

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:当所选图像太小时,laravel 会显示错误:

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我期待这样的错误消息: The Admin Image width cannot be less than 600pxThe Admin Image height cannot be less than 600px

here 'Admin image' is the attribute name & '600' is the value I given in rules这里“管理图像”是属性名称,“600”是我在规则中给出的值

So, I wanted to make a custom error message for min_width and max_width in custom messages array on validation.php, that look like the following:所以,我想在validation.php的custom消息数组中为min_widthmax_width创建一个自定义错误消息,如下所示:

'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.但不幸的是,这不起作用,Laravel 继续显示默认消息。

Please understand that请理解

I need 2 separate error message for dimensions:min_width=600 & dimensions:min_height=600我需要 2 个单独的dimensions:min_width=600错误消息dimensions:min_width=600dimensions: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任何帮助将不胜感激

Add the custom error message in validation.php as below:validation.php添加自定义错误信息如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM