簡體   English   中英

Laravel自定義驗證消息作為數組導致錯誤

[英]Laravel Custom Validation Messages as Array Cause Errors

在一個包中,我試圖在與Laravel的默認文件分開的文件中包含自定義驗證錯誤消息。 我復制了文件,更改了值(沒有觸摸鍵),在服務提供商中正確加載,一切正常。 基本驗證(如requirednumericemail等)在我的文件中顯示文本。 現在,我嘗試使用size規則,但由於錯誤消息在數組中,因此在使用$errors->get( 'content.reasons' )時,它會給出此異常:

Array to string conversion in MessageBag.php (line 246)

這里是代碼的重要部分

調節器

$validator = Validator::make( $request->all(), $rules, trans( 'admin::validation' ) );
if ( $validator->fails() ){
    return redirect()
        ->back()
        ->withInput()
        ->withErrors( $validator );
}

$request->all()

[
    // ...
    "content" => [
        "reasons" => [
            [...],
            [...]
        ]
    ],
    // ...
]

$rules

[
    // ...
    "content.reasons" => "required|size:3"
    // ...
]

trans( 'admin::validation' )

// Exact structure of Laravel validation.php
[
    // ...
    'size'                 => [
        'numeric' => 'The field must be :size.',
        'file'    => 'The field must be :size kilobytes.',
        'string'  => 'The field must be :size characters.',
        'array'   => 'The field must contain :size items.',
    ],
    // ...
]

重定向后

$errors = session()->get('errors') ?: new ViewErrorBag;
$field[ 'errors' ] = $errors->get( $dot_name ); // Exception thrown when $dot_name
                                                // equals `content.reasons`.

$errors

Illuminate\Support\ViewErrorBag {
    #bags: array:1 [
        "default" => Illuminate\Support\MessageBag {
            #messages: array:4 [
                "content.why_title" => array:1 [
                    0 => "The field is required."
                ]
                "content.reasons" => array:1 [
                    0 => array:4 [
                        "numeric" => "The field must be 3."
                        "file" => "The field must be 3 kilobytes."
                        "string" => "The field must be 3 characters."
                        "array" => "The field must contain 3 items."
                    ]
                ]
                "content.reasons.0.icon" => array:1 [
                    0 => "The field is required."
                ]
                "content.reasons.0.text" => array:1 [
                    0 => "The field is required."
                ]
            ]
            #format: ":message"
        }
    ]
}

我已經嘗試傳遞content.reasons.array但它返回一個空數組。

我也嘗試不傳遞任何消息(使用Laravel的默認值)並且它可以工作,但我真的需要自定義消息...

TL; DR:我們如何使用與Laravel相同的模式傳遞自定義消息?

我不知道你想做什么,但你可以為每個驗證字段/規則傳遞自定義消息,例如,在你的控制器中:

    //Error messages
    $messages = [
        "name.required" => "Name is required",
        "name.string" => "Name must be a string",
        "username.required" => "Username is required",
        "username.min" => "Username must be at least 6 characters",
        "email.required" => "Email is required",
        "email.email" => "Email is not valid"
    ];

    // validate the form data
    $validator = Validator::make($request->all(), [
            'name' => 'required|string',
            'username' => 'required|min:6',
            'email' => 'required|email'
        ], $messages);

    if ($validator->fails()) {
        return back()->withErrors($validator)->withInput();
    } else {
        //Some code here
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM