Laravel自定义验证消息作为数组导致错误

Kar*_*non 9 php validation laravel-5.4

在一个包中,我试图在与Laravel的默认文件分开的文件中包含自定义验证错误消息.我复制了文件,更改了值(没有触摸键),在服务提供商中正确加载,一切正常.基本验证,如required,numeric,email,等显示在我的文件中的文本.现在,我尝试使用该size规则,但由于错误消息在数组中,因此在使用时会给出此异常$errors->get( 'content.reasons' ):

Array to string conversion in MessageBag.php (line 246)
Run Code Online (Sandbox Code Playgroud)

这里是代码的重要部分

调节器

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

$request->all()

[
    // ...
    "content" => [
        "reasons" => [
            [...],
            [...]
        ]
    ],
    // ...
]
Run Code Online (Sandbox Code Playgroud)

$rules

[
    // ...
    "content.reasons" => "required|size:3"
    // ...
]
Run Code Online (Sandbox Code Playgroud)

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.',
    ],
    // ...
]
Run Code Online (Sandbox Code Playgroud)

重定向后

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

$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"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我试过传递,content.reasons.array但它返回一个空数组.

我也尝试不传递任何消息(使用Laravel的默认值)并且它可以工作,但我真的需要自定义消息...

TL; DR:我们如何使用与Laravel相同的模式传递自定义消息?

小智 1

我不知道您想做什么,但您可以为每个验证字段/规则传递自定义消息,例如在控制器中:

    //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
    }
Run Code Online (Sandbox Code Playgroud)