简体   繁体   English

Laravel中的自定义验证消息

[英]Custom Validation Message in Laravel

I need an error message that essentially says "You need to have checked at least one box in at least one multi-dropdown" 我需要一条错误消息,基本上说“你需要在至少一个多下拉列表中检查至少一个方框”

My five multi-dropdown names are; 我的五个多下拉名称是; Country, County, Gor, Locauth, Parlc. 国家,县,Gor,Locauth,Parlc。

My controller so far is; 到目前为止我的控制器是;

$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);

My problem is that I cannot see a way of returning just the one rule. 我的问题是我看不到只返回一条规则的方法。 It returns 5 very similar worded 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.

Not brilliant! 不太棒! Is there a way to return just one custom message? 有没有办法只返回一个自定义消息?

--- EDIT --- ---编辑---

I should add... The Array() code above is not the actual code, that was a print_r result of the actual code which creates those rules. 我应该添加...上面的Array()代码不是实际的代码,它是创建这些规则的实际代码的print_r结果。 I just thought it would make it easier to read and understand my problem. 我只是觉得它会让你更容易阅读和理解我的问题。 The actual code, if you're interested is this; 实际的代码,如果你感兴趣的是这个;

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

--- ANOTHER EDIT --- ---另一个编辑---

I already know about custom error messages like; 我已经知道自定义错误消息了;

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

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

But this will just give me five error messages instead of only one. 但这只会给我五条错误信息,而不只是一条。

You only need the rule on one field for it to work correctly. 您只需要在一个字段上使用该规则即可正常工作。 The easiest way is to create an empty field and apply the rule to it. 最简单的方法是创建一个空字段并将规则应用于它。 The field doesn't have to exist in the database schema or anywhere else in your application. 该字段不必存在于数据库模式中或应用程序中的任何其他位置。

public static $rules = [
    location => required_without_all:country,county,gor,locauth,parlc
];

Then customize the message in your language file. 然后在您的语言文件中自定义消息。

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

Now when all of the fields (or check boxes in your case) are empty, you will receive the error message. 现在,当所有字段(或您的案例中的复选框)都为空时,您将收到错误消息。 If one or more are filled in, there won't be any error. 如果填写了一个或多个,则不会有任何错误。

if(!valiate){
  return redirect()->back()->withError('Some Fields are Required');
}
$messages = [
    'required_without_all' => 'The :attribute field is required.',
];

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

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

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