【问题标题】:Laravel Max rule fails inccorrectlyLaravel Max 规则错误地失败
【发布时间】:2018-07-27 15:58:23
【问题描述】:

我为我的帐户控制器设置了这样的验证集:

$messages =  [
          'avatar.max' => 'this is my custom error message for max size'
        ];

        $validator = Validator::make($request->all(), [
            'username'  => [
              'required',
              Rule::unique('users')->ignore($user->id)
            ],
            'email'     => [
              'required',
              Rule::unique('users')->ignore($user->id)
            ],
            'password'  => 'confirmed',
            'avatar' => 'max:2000'
          ], $messages);

如果我尝试上传更大的文件,验证器不会返回我的自定义消息。我认为是因为失败的规则对于某些原因来说不是最大的。如果我转储错误,失败的错误如下所示:

#failedRules: array:1 [▼
    "avatar" => array:1 [▼
      "uploaded" => []
    ]
  ]

这是因为我的 php ini 文件只允许上传 2mb 吗?我认为如果文件大于 2mb,验证器会阻止文件上传?我是否需要将我的服务器设置为允许 10mb,然后让验证器停止它?如果我在 2mb 以下上传,它可以正常上传(但显然不会失败,因为它在 2mb 以下)

更新 - 表格的一部分:

<form class="" action="/account/edit"  method="post" enctype="multipart/form-data">

      <input name="_method" type="hidden" value="PUT">

      {{ csrf_field() }}

      <div class="account-content--item">
        <div class="account-content--left">Your Photo</div>
        <div class="account-content--right">
          <avatar class="profile--avatar" username="{{ $user->username }}" :size="80" src="{{ $user->avatar }}"></avatar>
          <input type="file" name="avatar" accept="image/*">
        </div>
      </div>
</form>

【问题讨论】:

  • 你能在刀片中显示表格吗?
  • @packy 如果有帮助,请为我的回答投票

标签: laravel


【解决方案1】:

您需要更改您的 PHP 设置以允许更大的上传。 PHP 对这些有很多限制。 POST 大小、上传大小等

编辑:/etc/php/7.0/fpm/php.ini 并将 post_max_size 和 upload_max_filesize 值更改为您需要的值。

; Maximum allowed size for uploaded files.
upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize
post_max_size = 40M

修改php.ini文件后,需要重启HTTP服务器才能使用新配置。

sudo service php7.0-fpm restart

如果您使用 nginx 作为 Web 服务器,您也应该更改 nginx 配置,方法是将以下行添加到 nginx 配置中的 http{..} 块并重新加载 nginx:

client_max_body_size 100m;
sudo service nginx restart

【讨论】:

  • 如果有影响,我会使用代客服务
猜你喜欢
  • 2019-01-19
  • 2021-04-05
  • 2015-04-04
  • 1970-01-01
  • 1970-01-01
  • 2020-08-31
  • 1970-01-01
  • 2019-12-23
  • 2020-03-22
相关资源
最近更新 更多