Packy
Packy

Reputation: 3583

Laravel Max rule fails inccorrectly

I have validation set like this for my account controller:

$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);

If I try to upload a bigger file the validator doesnt return my custom message. I think because the rule that is being failed isnt max for some resson. If I dump the errors the failed one looks like this:

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

Is this because my php ini file only allows 2mb upload? I thought the validator would prevent the file from being uploaded if its bigger than 2mb? Do I need to set my server to allow like 10mb then have the validator stop it? If I upload under 2mb it uploads fine (but obviously doesnt fail since its under 2mb)

Update - Part of the Form:

<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>

Upvotes: 1

Views: 42

Answers (1)

Milad Teimouri
Milad Teimouri

Reputation: 1211

You need to change your PHP settings to allow for larger uploads. PHP has limits on these for a number of things. POST size, upload size etc

Edit: /etc/php/7.0/fpm/php.ini and change post_max_size & upload_max_filesize values to what you need.

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

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

After modifying php.ini file, you need to restart your HTTP server to use new configuration.

sudo service php7.0-fpm restart

if your are using nginx as web server you should changes nginx config too by Add following line to http{..} block in nginx config and reload nginx:

client_max_body_size 100m;
sudo service nginx restart

Upvotes: 1

Related Questions