# Validator - upload files

**URL:** <https://discourse.cakephp.org/t/validator-upload-files/8482>\
**Category:** Need Help\
**Created:** [October 8, 2020, 5:09pm UTC](https://discourse.cakephp.org/t/validator-upload-files/8482 "2020-10-08T17:09:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [October 8, 2020, 5:09pm UTC](https://discourse.cakephp.org/t/validator-upload-files/8482/1 "2020-10-08T17:09:14Z")

</div>

Can you please advise me how to define a validator form, where I have the option to attach multiple files and only the mimetype image/jpeg is allowed.

For one file is:

```
     $validator
        ->allowEmptyFile('image_teaser')
        ->requirePresence('image_teaser', 'create')
        ->uploadedFile('image_teaser', [
            'types' => [
                'image/jpeg',
            ],
     ],".jpg");
```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [October 8, 2020, 5:47pm UTC](https://discourse.cakephp.org/t/validator-upload-files/8482/2 "2020-10-08T17:47:20Z")

</div>

Do you need multiple `image_teaser` files, to create a new record for each one? Or do you have multiple different fields in a single record that you need to upload for all at once (e.g. a teaser image and a banner image and a footer image)? The solution is quite different for those two scenarios.

Also, can you share what this part of your form currently looks like?

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [October 8, 2020, 5:51pm UTC](https://discourse.cakephp.org/t/validator-upload-files/8482/3 "2020-10-08T17:51:26Z")

</div>

The form:

```
echo $this->Form->control('image_gallery[]', [
    'label' => 'Galery *.jpg',
    'type' => 'file',
    'multiple' => 'multiple',
]);

```

And the validator should verify that only \* .jpg files are selected

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [January 3, 2021, 11:21am UTC](https://discourse.cakephp.org/t/validator-upload-files/8482/4 "2021-01-03T11:21:45Z")

</div>

So how do I validate a field that contains multiple images?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 3, 2021, 2:57pm UTC](https://discourse.cakephp.org/t/validator-upload-files/8482/5 "2021-01-03T14:57:05Z")

</div>

I would think that each of the images would result in a separate record being created in some other table, and you’d do validation of them on a per-record basis.

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [January 3, 2021, 3:12pm UTC](https://discourse.cakephp.org/t/validator-upload-files/8482/6 "2021-01-03T15:12:17Z")

</div>

I do not save images to the database, only to DISK. However, I need if I select multiple images at once:

`<input type="file" name="image_gallery[]" multiple="multiple" id="image-gallery">`

so that I can verify that I meet the type of jpg.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 3, 2021, 7:42pm UTC](https://discourse.cakephp.org/t/validator-upload-files/8482/7 "2021-01-03T19:42:37Z")

</div>

You’ll need to make a [custom validation function](https://book.cakephp.org/3/en/core-libraries/validation.html#using-custom-validation-rules) for that, then, though you could perhaps call the underlying code for `uploadedFile` from your function.

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [February 7, 2021, 12:12pm UTC](https://discourse.cakephp.org/t/validator-upload-files/8482/8 "2021-02-07T12:12:38Z")

</div>

Can’t display a message below the field error field, probably because it’s called `image_gallery[]`, please how do I display an error message for a given field?

#### Validation errors

```
[
    'page_content' => [
        'image_gallery' => [
           'uploadedFile' => 'Allow: .jpg, .png',
        ], 
    ], 
],
```

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [October 22, 2022, 3:57pm UTC](https://discourse.cakephp.org/t/validator-upload-files/8482/9 "2022-10-22T15:57:13Z")

</div>

Please don’t you know how to display the error message under the field: `image_gallery[]`

I still didn’t think about it.  
Thank you

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [October 27, 2022, 10:45am UTC](https://discourse.cakephp.org/t/validator-upload-files/8482/10 "2022-10-27T10:45:17Z")

</div>

Here is the solution:

```auto
$this->Form->control('image_gallery', [
    'name' => 'image_gallery[]',
    'label' => 'Galery *.jpg',
    'type' => 'file',
    'multiple' => true,
]);

```
