【问题标题】:Do custom error messages in Laravel 4.2在 Laravel 4.2 中自定义错误消息
【发布时间】:2015-12-22 20:20:38
【问题描述】:

我是 Larvel 4.2 的新手!如何在 Laravel 4.2 中自定义错误消息?我把这些代码放在哪里?我一直在使用默认值,我有点想使用自己的。

【问题讨论】:

  • 当您在表单上使用验证时,这是用于自定义错误消息吗?
  • 是的,我在验证表单时尝试使用它
  • 那么下面的答案对你来说是完美的,只需将代码放在表单使用的控制器函数中,它就会实现你定义的自定义错误消息。
  • 谢谢,我只是不知道该放在哪里
  • 我已经添加了一个答案来告诉你如何使用它。

标签: laravel laravel-4 laravel-validation


【解决方案1】:

你有没有尝试过? http://laravel.com/docs/4.2/validation#custom-error-messages

你用过谷歌吗?检查文档(官方)它拥有一切。少一点懒惰。

$messages = array(
    'required' => 'The :attribute field is required.',
);

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

【讨论】:

  • 如果我听起来很懒惰和愚蠢,我很抱歉。谢谢,我只是不知道放在哪里
  • 我没说你傻。一开始自己做某事是最好的老师。然后问...享受 Laravel - 它真的是很棒的东西。
【解决方案2】:

为了补充 slick 给出的答案,您可以在控制器内的 store 函数的真实示例中使用它:

public function store(Request $request)
    {    
        $validator = Validator::make($request->all(), [
            'id1' => 'required|between:60,512',
            'id2' => 'required|between:60,512',
            'id3' => 'required|unique:table',
        ], [
            'id1.required' => 'The first field is empty!',
            'id2.required' => 'The second field is empty!',
            'id3.required' => 'The third field is empty!',
            'id1.between' => 'The first field must be between :min - :max characters long.',
            'id2.between' => 'The second answer must be between :min - :max characters long.',
            'id3.unique' => 'The third field must be unique in the table.',
        ]);

        if ($validator->fails()) {
            return Redirect::back()
                ->withErrors($validator)
                ->withInput();
        }

        //... Do something like store the data entered in to the form
}

id 应该是您要验证的表单中字段的 ID。

您可以查看所有可以使用的规则here

【讨论】:

    猜你喜欢
    • 2018-08-12
    • 2015-12-28
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 2017-08-17
    • 2019-10-28
    相关资源
    最近更新 更多