Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel中的自定义验证消息_Php_Laravel_Laravel 5 - Fatal编程技术网

Php Laravel中的自定义验证消息

Php Laravel中的自定义验证消息,php,laravel,laravel-5,Php,Laravel,Laravel 5,我需要一条错误消息,本质上说“您需要在至少一个多下拉列表中选中至少一个框” 我的五个多下拉名称是;国家、县、Gor、Locauth、Parlc 到目前为止,我的控制器是 $rules = Array ( [country] => required_without_all:county,gor,locauth,parlc [county] => required_without_all:country,gor,locauth,parlc [gor] => requir

我需要一条错误消息,本质上说“您需要在至少一个多下拉列表中选中至少一个框”

我的五个多下拉名称是;国家、县、Gor、Locauth、Parlc

到目前为止,我的控制器是

$rules = Array
(
  [country] => required_without_all:county,gor,locauth,parlc
  [county] => required_without_all:country,gor,locauth,parlc
  [gor] => required_without_all:country,county,locauth,parlc
  [locauth] => required_without_all:country,county,gor,parlc
  [parlc] => required_without_all:country,county,gor,locauth
)

$validator = \Validator::make($input, $rules);
我的问题是,我看不到一种只返回一条规则的方法。它返回5条措辞非常相似的规则

The country field is required when none of county / gor / locauth / parlc are present.
The county field is required when none of country / gor / locauth / parlc are present.
The gor field is required when none of country / county / locauth / parlc are present.
The locauth field is required when none of country / county / gor / parlc are present.
The parlc field is required when none of country / county / gor / locauth are present.
不聪明!有没有办法只返回一条自定义消息

---编辑---

我应该补充说。。。上面的
Array()
代码不是实际代码,这是创建这些规则的实际代码的打印结果。我只是觉得这样可以更容易地阅读和理解我的问题。如果你感兴趣的话,实际的代码是这样的

$fields = ['country','county','gor','locauth','parlc'];
    $rules = [];
    foreach ($fields as $i => $field) {
        $rules[$field] = 'required_without_all:' . implode(',', array_except($fields, $i));
    }
---另一次编辑---

我已经知道自定义错误消息,如

$messages = [
'required' => 'The :attribute field is required.',
];

$validator = Validator::make($input, $rules, $messages);

但这只会给我五条错误消息,而不是一条。

您只需要一个字段上的规则,它就可以正常工作。最简单的方法是创建一个空字段并对其应用规则。该字段不必存在于数据库模式或应用程序中的任何其他位置

$messages = [
    'required_without_all' => 'The :attribute field is required.',
];

$validator = Validator::make($input, $rules, $messages);
public static $rules = [
    location => required_without_all:country,county,gor,locauth,parlc
];
然后在语言文件中自定义消息

'custom' => array(
    'location' => array(
        'required_without_all' => 'At least one location field is required',
    ),
),

现在,当所有字段(或案例中的复选框)都为空时,您将收到错误消息。如果填写了一个或多个,则不会有任何错误。

请查看文档。可以设置每个字段的验证消息。但它不会只打印5条自定义错误消息吗?我只想要一个来捕获所有五个…为什么要使用
foreach
?如果你只想得到一条错误信息,为什么你有5条规则?为什么不创建一个验证规则来测试五个输入?而不是手动验证每个字段?因为我必须将每个字段与其他4个字段进行比较。我认为最好的方法是使用
required\u而不使用\u all
。你不能只用一条规则就做到这一点,是吗?你必须这样做5次。正如我已经说过的——我会更新我的问题——这仍然会输出5条错误消息,我只想要一条。谢谢,虽然这个答案有什么问题,请加上一个评论,如果你大拇指向下。拼写错误我认为:p
'custom' => array(
    'location' => array(
        'required_without_all' => 'At least one location field is required',
    ),
),