Allow no file when validation an uploaded file - CakePHP 2


I’m uploading an image for an article, and in the model, i set some validation rules for this 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

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

1 Like

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

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”

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

1 Like

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

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