File validation rule object in Laravel 9.x — Amit Merchant — A blog on PHP, JavaScript, and more

0
21

Validating things plays a crucial role in any web application. In Laravel, you can validate the incoming request data using the validate() method. This method accepts an array of validation rules as its first argument. These rules are used to validate the incoming request data.

Now, traditionally, if you want to validate a file, you can use the file rule. For example, let’s say you want to validate a file named avatar in the incoming request data, you can do so like so.

$request->validate([
    'avatar' => ['file', 'image', 'max:2048'],
]);

As you can tell, here, we’re using the file rule to validate the incoming file. We are also using the image and max rules to validate that the file is an image and its size respectively.

This is fine but in Laravel 9.x, you can use the File validation rule object to validate the incoming file. This object is a wrapper around the file rule. So, you can use it like that.

Using the File validation rule object

Let’s say you want to validate the incoming file named avatar in the incoming request data that should be an image that shouldn’t be larger than 2MB, you can use the File validation rule object like so.

$request->validate([
    'avatar' => ['required', 'file', File::image()->atMost(2048)],
]);

We can fluently chain supported rules to the File validation rule object. Here’s one more example.

$request->validate([
    'avatar' => [
        'required', 
        File::image()
                ->atLeast(2 * 1024)
                ->smallerThan(12 * 1024)
                ->dimensions(
                    Rule::dimensions()
                        ->maxWidth(1000)
                        ->maxHeight(500)
                )
    ],
]);

Here are all the supported rules that you can use with the File validation rule object.

Supported rules

The following methods are available on the rule:

  • ::types – equivalent to the mimetypes and mimes rules
  • ::image – equivalent to the image rule
  • exactly – equivalent to the size rule
  • between – equivalent to the between rule
  • atLeast – equivalent to the min rule
  • atMost – equivalent to the max rule
  • largerThan – equivalent to the min rule + 1kb
  • smallerThan – equivalent to the max rule – 1kb
  • dimensions – only available after calling ::imageallows specifying Rule::dimensions() constraints

Defining File::defaults() rules

If you want to use the default rules that are used by the File validation rule object, you can define the File::defaults() method in the boot method of your application AppServiceProvider like so.

use IlluminateSupportFacadesValidator;
use IlluminateValidationRulesFile;

public function boot()

    File::defaults(
        fn (): File => File::image()
                ->types(['png', 'jpeg', 'jpg'])
                ->atMost(2048);
    );

And then, you can use it when validating the incoming request data like this.

$request->validate([
    'avatar' => ['required', File::defaults()],
]);

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here