【问题标题】:How to set laravel custom validation message with laravel rules如何使用 laravel 规则设置 laravel 自定义验证消息
【发布时间】:2018-01-18 14:02:08
【问题描述】:

让我先展示我的代码。这是我的控制器功能代码

public function save(Request $request) {
    try {
        $this->validate($request, Venue::rules()); // Validation  Rules 
        $venue = Venue::saveOrUpdate($request);
        if($venue !== false) {
            if($request->get('continue', false)) {
                return redirect()->route('admin.venue.edit', ['id' => $venue->id])->with('success', trans('admin.venue.save_success'));
            } else {
                return redirect()->route('admin.venue.index')->with('success', trans('admin.venue.save_success'));
            }
        } else {
            return back()->with('error', "Unable to save venue")->withInput();
        }

    } catch (\Exception $ex) {
        return back()->with('error', "Unable to save venue")->withInput();
    }
}

这是我的模型功能代码

public static function rules($id = '') {
    return [
        'name' => 'required|string|max:255',
        'logo' => 'required',
        'status' => 'required|string|in:' . implode(",", Venue::STATUSES),
        'venue_type_id' => 'required|string|not_in:0',
         'client_id' => 'required|string|not_in:0',
    ];
}

所以现在当我提交表单验证显示消息时。我想更改此消息。我该怎么做。

让我展示带有验证信息的表单:

【问题讨论】:

标签: php laravel


【解决方案1】:

您可以通过覆盖messages() 方法来自定义form request 使用的error messages。在Venue 类上添加自定义messages,如下所示-

public static function messages($id = '') {
return [
    'name.required' => 'You must enter your name',
    'logo.required' => 'You must upload logo',
    'key.rules' => 'your messages'
];

然后在你的控制器上添加messages 作为第三个parameter like-

$this->validate($request, Venue::rules(), Venue::messages());

【讨论】:

  • 快速问题'client_id' => 'required|string|not_in:0' 是 id 当我设置`'client_id.required' => 'Select Client',`它不起作用。
  • 在验证之前尝试 dd(Input::all()) 看看你得到了什么作为 client_id
  • "client_id" => "0" gt 这个。未选择客户端时。
  • 那么它肯定不是空的,这就是为什么没有必需的验证错误
  • 有什么方法可以设置,如果 id 为 0,则在我们定义 name.required 的自定义消息中计为 null,例如 client_id.required.somethin..like that?
【解决方案2】:

您可以自定义验证消息,

转到resources->lang->en->validation.php

你看,

'custom' => [
        'attribute-name' => [
            'rule-name' => 'custom-message',
        ],
    ],

根据需要进行编辑。

【讨论】:

  • 是否可以在我在模型上创建的规则上添加新的客户消息。
  • Gt the answer.thanx
【解决方案3】:

我就是这样做的,可以作为指导。在您的表单中,您基本上有 4 个输入字段,假设它们被命名为name、client、logo 和venue_type。控制器中验证表单请求的函数如下所示:
N.B - 你应该放 - 使用验证器; 使用 Illuminate\Http\Request; - 在你的班级顶部

公共函数 validateFormRequest($request){ 尝试 { //在此处指定您的自定义消息 $消息 = [ 'required' => ':attribute 字段是必需的', 'string' => ':attribute 必须是文本格式', 'file' => ':attribute 必须是一个文件', 'mimes' => ':attribute 支持的文件格式是 :mimes', 'max' => ':attribute 的最大长度必须是 :max', ]; $validator = Validator::make($request->all(), [ '名称' => '必需|字符串|最大值:75', '客户端' => '必需|字符串|最大值:75', 'logo' => '必需|文件|mimes:jpeg,png,jpg,gif', 'venue_type' => '必需|字符串', ], $消息); if($validator->fails()){ // Validation Failed..log 错误或返回错误到视图/刀片 } 别的{ // 验证通过..返回真或正信息。即可以保存请求 } }catch (例外 $ex){ //记录您的错误或向您的视图/刀片返回一些错误消息 } }

【讨论】:

    【解决方案4】:

    您可以像这样添加自定义错误。

    $validation->errors()->add('error_input', 'error text');
    
    return redirect()->back()->withInput()->withErrors($validation);
    

    return redirect()->back()->withInput()->withErrors(['error_input'=> 'error text');
    

    【讨论】:

    • 您可以将此错误添加到您的验证中并返回 redirect()->back()->withInput()->withErrors($validation);
    • 有人可以发布可编辑的代码吗?因为我 gt 错误Undefined variable: validation
    • 所以如果我的表单中有 15 个字段..那么我需要在返回时设置吗?
    • 使用 Illuminate\Support\Facades\Validator; $validation = Validator::make($request->all(), [ 'user_name' => 'required', 'user_last_name' => 'required', 'user_phone' => 'required', 'user_email' => ' required|email', 'user_agreement' => 'required', 'address' => 'required', 'quantity' => 'required|integer|min:1|max:1000', ]);这是自定义验证器示例,您首先使用它,然后添加您的自定义验证器
    猜你喜欢
    • 1970-01-01
    • 2015-09-07
    • 2023-03-20
    • 1970-01-01
    • 2021-08-30
    • 2017-04-11
    • 2018-02-18
    • 2020-10-18
    • 2017-12-01
    相关资源
    最近更新 更多