Skip to content

Commit 3388982

Browse files
committed
Add documentation to validate multiple upload file
1 parent 97779a4 commit 3388982

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

README.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,8 @@ The field under validation must be present and not empty only when all of the ot
440440

441441
<details><summary><strong>uploaded_file</strong>:min_size,max_size,extension_a,extension_b,...</summary>
442442

443-
This rule will validate `$_FILES` data, but not for multiple uploaded files.
444-
Field under this rule must be following rules below to be valid:
443+
This rule will validate data from `$_FILES`.
444+
Field under this rule must be follows rules below to be valid:
445445

446446
* `$_FILES['key']['error']` must be `UPLOAD_ERR_OK` or `UPLOAD_ERR_NO_FILE`. For `UPLOAD_ERR_NO_FILE` you can validate it with `required` rule.
447447
* If min size is given, uploaded file size **MUST NOT** be lower than min size.
@@ -455,6 +455,56 @@ Here are some example definitions and explanations:
455455
* `uploaded_file:0,1M`: uploaded file size must be between 0 - 1 MB, but uploaded file is optional.
456456
* `required|uploaded_file:0,1M,png,jpeg`: uploaded file size must be between 0 - 1MB and mime types must be `image/jpeg` or `image/png`.
457457

458+
Optionally, if you want to have separate error message between size and type validation.
459+
You can use `mimes` rule to validate file types, and `min`, `max`, or `between` to validate it's size.
460+
461+
For multiple file upload, PHP will give you undesirable array `$_FILES` structure ([here](http://php.net/manual/en/features.file-upload.multiple.php#53240) is the topic). So we make `uploaded_file` rule to automatically resolve your `$_FILES` value to be well-organized array structure. That means, you cannot only use `min`, `max`, `between`, or `mimes` rules to validate multiple file upload. You should put `uploaded_file` just to resolve it's value and make sure that value is correct uploaded file value.
462+
463+
For example if you have input files like this:
464+
465+
```html
466+
<input type="file" name="photos[]"/>
467+
<input type="file" name="photos[]"/>
468+
<input type="file" name="photos[]"/>
469+
```
470+
471+
You can simply validate it like this:
472+
473+
```php
474+
$validation = $validator->validate($_FILES, [
475+
'photos.*' => 'uploaded_file:0,2M,jpeg,png'
476+
]);
477+
478+
// or
479+
480+
$validation = $validator->validate($_FILES, [
481+
'photos.*' => 'uploaded_file|max:2M|mimes:jpeg,png'
482+
]);
483+
```
484+
485+
Or if you have input files like this:
486+
487+
```html
488+
<input type="file" name="images[profile]"/>
489+
<input type="file" name="images[cover]"/>
490+
```
491+
492+
You can validate it like this:
493+
494+
```php
495+
$validation = $validator->validate($_FILES, [
496+
'images.*' => 'uploaded_file|max:2M|mimes:jpeg,png',
497+
]);
498+
499+
// or
500+
501+
$validation = $validator->validate($_FILES, [
502+
'images.profile' => 'uploaded_file|max:2M|mimes:jpeg,png',
503+
'images.cover' => 'uploaded_file|max:5M|mimes:jpeg,png',
504+
]);
505+
```
506+
507+
Now when you use `getValidData()` or `getInvalidData()` you will get well array structure just like single file upload.
458508

459509
</details>
460510

0 commit comments

Comments
 (0)