【问题标题】:laravel 5 validation custom error message not appearlaravel 5验证自定义错误消息不出现
【发布时间】:2018-07-17 12:00:43
【问题描述】:

如果用户上传的图片大小超过 4 MB,我想显示自定义验证错误消息。但是,一旦提交了 4MB 以上的图像,它会显示默认错误消息:“文件名上传失败。”。以下是我在控制器中的代码:

$messages = [
            'fileName' => 'Image maximum size exceed. ',
];

$validator = Validator::make($request->all(), [
            'fileName' => 'max:4096', 
        ], $messages);

if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors());
        }

这是刀片文件中的 HTML 代码:

<input type="file" name="fileName">

【问题讨论】:

  • 在您的 $messages 数组中使用 fileName.max
  • @rkj done as 'fileName.max' => '图像最大尺寸超出。 ', ---- 但显示相同的消息。
  • 您是否在 php.ini 文件中设置了更高的 max_file_size 值?
  • 也许是这个? resources/lang/en/validation.php 'custom' => [ 'attribute-name' => [ 'fileName' => 'custom-message', ], ],
  • @Bas 我想通过在控制器中写入来做 custm 消息

标签: laravel laravel-5


【解决方案1】:

我知道这是一个老问题,但我必须在这里发布这个答案。这对我有用

$messages = [
            'fileName.uploaded' => 'Image maximum size exceed. ',
];

【讨论】:

    【解决方案2】:

    我用了这条线:

    return redirect()->back()->withErrors($validator->customMessages);
    

    它解决了这个问题。

    【讨论】:

    • 如果我需要验证多个规则,这不起作用,它只返回customMessage
    猜你喜欢
    • 2015-09-07
    • 2021-02-24
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    相关资源
    最近更新 更多