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',
]);
当所选图像太小时,laravel显示错误:
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.
我期望出现类似以下的错误消息:
The Admin Image width cannot be less than 600px
和
The Admin Image height cannot be less than 600px
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
此处管理映像"是属性名称& "600"是我在规则中给出的值
here 'Admin image' is the attribute name & '600' is the value I given in rules
因此,我想为validation.php上的custom
消息数组中的min_width
和max_width
创建自定义错误消息,如下所示:
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:
'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',
],
但不幸的是,这不起作用& Laravel继续显示默认消息.
But unfortunately that doesn't work & Laravel continues to show the default message.
请理解
就像我在自定义错误消息数组中尝试过的那样.
like I tried in the custom error messages array.
我知道这很简单,但是我做错了事.
I know this is very simple but I'm doing something wrong.
任何帮助将不胜感激
推荐答案
在validation.php
中添加自定义错误消息,如下所示:
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'
]
]
这篇关于Laravel验证规则的自定义错误消息:尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!