【问题标题】:Set One Custom message for multiple field in laravel form validation在 laravel 表单验证中为多个字段设置一条自定义消息
【发布时间】:2014-10-31 16:54:18
【问题描述】:

让我在表单中有 3 个字段。

我为表单验证编写此代码。

$rules = array(
                'field_1' => 'required',
                'field_2' => 'required',
                'field_3' => 'required',
            );


$messages = array(
                'field_1.required' => 'Please fill up all value',
                'field_2.required' => 'Please fill up all value',
                'field_3.required' => 'Please fill up all value',
            );

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

现在,如果用户用空白填写表单 2 字段,则验证器会返回两条错误消息。如果用户用空白填写表单 3 字段,则验证器返回三个错误消息。但我只想为所有字段显示一条消息。

【问题讨论】:

  • 所以你想让它说“某些字段为空”,如表示多个字段或返回一串错误消息,如“字段 1 为空,字段 2 为空”等
  • 详细说明“我只想为所有字段显示一条消息”
  • @Brian 是的。你是对的
  • 好吧,我是对的,但是关于我的哪个场景:)
  • @Brian “有些字段是空的”是空的。但我想显示此消息一次。

标签: validation laravel-4


【解决方案1】:

你可以改成

$messages = array(
    'required' => 'Please fill up all value',
);
validator = Validator::make(Input::all(), $rules, $messages);

让你的视图只输出第一条错误信息。

{{ $errors->first(null,'<div class="alert alert-danger">:message</div>') }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 2012-11-27
    • 2019-08-10
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    相关资源
    最近更新 更多