# Allow no file when validation an uploaded file - CakePHP 2

**URL:** <https://discourse.cakephp.org/t/allow-no-file-when-validation-an-uploaded-file-cakephp-2/3653>\
**Category:** Need Help\
**Created:** [December 15, 2017, 2:51pm UTC](https://discourse.cakephp.org/t/allow-no-file-when-validation-an-uploaded-file-cakephp-2/3653 "2017-12-15T14:51:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayaou](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/ayaou/32/477_2.png) [@ayaou](https://discourse.cakephp.org/u/ayaou)\
**Post date:** [December 15, 2017, 2:51pm UTC](https://discourse.cakephp.org/t/allow-no-file-when-validation-an-uploaded-file-cakephp-2/3653/1 "2017-12-15T14:51:53Z")

</div>

Hi,

I’m uploading an image for an article, and in the model, i set some validation rules for this image.

```
//View
$this->Form->input('Post.image');

//Model Post
public $validate = array(
    'image' => array(
        'extension' => array(
            'rule' => array('extension', array('jpeg', 'jpg', 'png', 'gif', 'bmp')),
            'message' => "L'extension de l'image envoyée n'est pas valide"
        ),
        'uploadError' => array(
            'rule' => array('uploadError', true),
            'message' => "Une erreur est survenue lors de l'upload de l'image"
        ),
        'mimeType' => array(
            'rule' => array('mimeType', array('image/png', 'image/jpeg', 'image/jpeg', 'image/jpeg', 'image/gif', 'image/bmp')),
            'message' => "Type de l'image invalide"
        ),
        'fileSize' => array(
            'rule' => array('fileSize', '<=', '2MB'),
            'message' => "La taille de l'image ne doit pas dépasser 2MB"
        )
    )
);

```

The image is not `required`, but the input generated in html view contains the `required` attribute, i fix that by adding `allowEmpty => true` on every rule. But still, the validation doesnt pass!

How can i fix this ?

Thank you

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [December 15, 2017, 3:27pm UTC](https://discourse.cakephp.org/t/allow-no-file-when-validation-an-uploaded-file-cakephp-2/3653/2 "2017-12-15T15:27:30Z")

</div>

What is the error message you get? Is NULL allowed in the database table filed where you save the image?

---

<div class="post-metadata">

**Author:** ![ayaou](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/ayaou/32/477_2.png) [@ayaou](https://discourse.cakephp.org/u/ayaou)\
**Post date:** [December 15, 2017, 3:40pm UTC](https://discourse.cakephp.org/t/allow-no-file-when-validation-an-uploaded-file-cakephp-2/3653/3 "2017-12-15T15:40:47Z")

</div>

The null value is permitted in db, that’s why this field is optional 😃

The error i’am getting is a validation error for the first rule (mimeTypes) : “L’extension de l’image envoyée n’est pas valide”

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [December 15, 2017, 4:50pm UTC](https://discourse.cakephp.org/t/allow-no-file-when-validation-an-uploaded-file-cakephp-2/3653/4 "2017-12-15T16:50:12Z")

</div>

It complains about the extension. Here in your example the allowempty rule is missing. Can you update your code to contain that?

---

<div class="post-metadata">

**Author:** ![ayaou](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/ayaou/32/477_2.png) [@ayaou](https://discourse.cakephp.org/u/ayaou)\
**Post date:** [December 15, 2017, 5:17pm UTC](https://discourse.cakephp.org/t/allow-no-file-when-validation-an-uploaded-file-cakephp-2/3653/5 "2017-12-15T17:17:45Z")

</div>

As i mentioned in the description, i added `allowEmpty` key, and all what it did, is removing the `required` attribute from the html file input, but the cakePHP validation didn’t succeed although I used `allowEmpty` to `true`

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [December 15, 2017, 5:59pm UTC](https://discourse.cakephp.org/t/allow-no-file-when-validation-an-uploaded-file-cakephp-2/3653/6 "2017-12-15T17:59:34Z")

</div>

Without seeing your code I am not able to help you further.
