【问题标题】:Laravel Custom Validation Messages as Array Cause ErrorsLaravel 自定义验证消息作为数组导致错误
【发布时间】:2018-01-12 15:13:03
【问题描述】:

在一个包中,我试图在一个与 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 相同架构的自定义消息?

【问题讨论】:

  • 这意味着你正在尝试echoarray
  • @vietnguyen09 我知道它是什么意思,问题是我如何传递一个与 Laravel 结构相同的自定义错误消息数组,并且仍然适用于使用数组的规则作为消息(如size)。希望它能澄清问题。
  • 前几天我遇到了类似的错误,这是我通过'size' => 'The field must be :size.' 让它工作的方法,但是通过这种方式,我不得不告别其他类型。
  • @vietnguyen09 是的,这也是我所做的。这似乎更像是一个错误,而不是关于 SO 的合适问题。我已将其作为问题发布在 Laravel GitHub 页面 (github.com/laravel/framework/issues/20754) 上。

标签: php validation laravel-5.4


【解决方案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
    }

【讨论】:

  • 是的,我知道这一点,但我需要它比显式传递错误消息更加动态。还是谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-20
  • 2017-11-19
  • 2017-05-30
  • 2021-02-24
  • 2020-02-26
  • 2015-09-07
  • 1970-01-01
相关资源
最近更新 更多