在 Laravel 4.2 中执行自定义错误消息

Do custom error messages in Laravel 4.2

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

你试过了吗? http://laravel.com/docs/4.2/validation#custom-error-messages

你用过Google吗?检查文档(官方),它包含所有内容。少偷懒。

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

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

为了补充 slick 给出的答案,以下是如何在控制器内的存储功能的真实示例中使用它:

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