Skip to content

Commit dbb8b04

Browse files
[11.x] Improve File validation structure and clarify File::image() usage (#10142)
* Update validation.md * Update validation.md * Update validation.md * Update validation.md * Update validation.md * Remove extra new line * Update validation.md * Update validation.md * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent ecc940f commit dbb8b04

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

validation.md

+46-25
Original file line numberDiff line numberDiff line change
@@ -1242,15 +1242,18 @@ A _ratio_ constraint should be represented as width divided by height. This can
12421242

12431243
'avatar' => 'dimensions:ratio=3/2'
12441244

1245-
Since this rule requires several arguments, you may use the `Rule::dimensions` method to fluently construct the rule:
1245+
Since this rule requires several arguments, it is often more convenient to use use the `Rule::dimensions` method to fluently construct the rule:
12461246

12471247
use Illuminate\Support\Facades\Validator;
12481248
use Illuminate\Validation\Rule;
12491249

12501250
Validator::make($data, [
12511251
'avatar' => [
12521252
'required',
1253-
Rule::dimensions()->maxWidth(1000)->maxHeight(500)->ratio(3 / 2),
1253+
Rule::dimensions()
1254+
->maxWidth(1000)
1255+
->maxHeight(500)
1256+
->ratio(3 / 2),
12541257
],
12551258
]);
12561259

@@ -2196,42 +2199,60 @@ Laravel provides a variety of validation rules that may be used to validate uplo
21962199
],
21972200
]);
21982201

2199-
If your application accepts images uploaded by your users, you may use the `File` rule's `image` constructor method to indicate that the uploaded file should be an image. In addition, the `dimensions` rule may be used to limit the dimensions of the image:
2200-
2201-
use Illuminate\Support\Facades\Validator;
2202-
use Illuminate\Validation\Rule;
2203-
use Illuminate\Validation\Rules\File;
2202+
<a name="validating-files-file-types"></a>
2203+
#### Validating File Types
22042204

2205-
Validator::validate($input, [
2206-
'photo' => [
2207-
'required',
2208-
File::image()
2209-
->min(1024)
2210-
->max(12 * 1024)
2211-
->dimensions(Rule::dimensions()->maxWidth(1000)->maxHeight(500)),
2212-
],
2213-
]);
2205+
Even though you only need to specify the extensions when invoking the `types` method, this method actually validates the MIME type of the file by reading the file's contents and guessing its MIME type. A full listing of MIME types and their corresponding extensions may be found at the following location:
22142206

2215-
> [!NOTE]
2216-
> More information regarding validating image dimensions may be found in the [dimension rule documentation](#rule-dimensions).
2207+
[https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types](https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types)
22172208

22182209
<a name="validating-files-file-sizes"></a>
2219-
#### File Sizes
2210+
#### Validating File Sizes
22202211

22212212
For convenience, minimum and maximum file sizes may be specified as a string with a suffix indicating the file size units. The `kb`, `mb`, `gb`, and `tb` suffixes are supported:
22222213

22232214
```php
2224-
File::image()
2215+
File::types(['mp3', 'wav'])
22252216
->min('1kb')
2226-
->max('10mb')
2217+
->max('10mb');
22272218
```
22282219

2229-
<a name="validating-files-file-types"></a>
2230-
#### File Types
2220+
<a name="validating-files-image-files"></a>
2221+
#### Validating Image Files
22312222

2232-
Even though you only need to specify the extensions when invoking the `types` method, this method actually validates the MIME type of the file by reading the file's contents and guessing its MIME type. A full listing of MIME types and their corresponding extensions may be found at the following location:
2223+
To validate that uploaded files are images, you can use the `File` rule's `image` constructor method. The `File::image()` rule ensures that the file under validation is an image (jpg, jpeg, png, bmp, gif, svg, or webp):
22332224

2234-
[https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types](https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types)
2225+
```php
2226+
use Illuminate\Support\Facades\Validator;
2227+
use Illuminate\Validation\Rule;
2228+
use Illuminate\Validation\Rules\File;
2229+
2230+
Validator::validate($input, [
2231+
'photo' => [
2232+
'required',
2233+
File::image(),
2234+
],
2235+
]);
2236+
```
2237+
2238+
<a name="validating-files-image-dimensions"></a>
2239+
#### Validating Image Dimensions
2240+
2241+
You may also validate the dimensions of an image. For example, to validate that an uploaded image is at least 1000 pixels wide and 500 pixels tall, you may use the `dimensions` rule:
2242+
2243+
```php
2244+
use Illuminate\Validation\Rule;
2245+
use Illuminate\Validation\Rules\File;
2246+
2247+
File::image()->dimensions(
2248+
Rule::dimensions()
2249+
->maxWidth(1000)
2250+
->maxHeight(500)
2251+
)
2252+
```
2253+
2254+
> [!NOTE]
2255+
> More information regarding validating image dimensions may be found in the [dimension rule documentation](#rule-dimensions).
22352256
22362257
<a name="validating-passwords"></a>
22372258
## Validating Passwords

0 commit comments

Comments
 (0)