I have a problem with my file upload validation in CakePHP.

When I add a new record with an image upload field...

   - ... image should be required.
   - ... image file extension sould be jpg, png or gif.

When I edit an existing record with an image upload field...

   - ... image is not required.
   - ... when image is choosen: image file extension sould be jpg, png or 
   gif.

Here's my best model code attempt so far:

<?php


class Outlet extends CoasterCmsAppModel
{
    public $validate = array(
        'name' => array(
            'rule' => 'notEmpty', // verplicht
            'message' => 'Name is required.',
            'allowEmpty' => true
        ),
        'intro' => array(
            'rule' => 'notEmpty', // verplicht
            'message' => 'Intro is required.'
        ),
        'photo' => array(
            'validFileSize' => array( // zelf gekozen naam van de regel
                'rule' => array('filesize', '>', 0), // verplicht
                'on' => 'create',
                'message' => 'Photo is required.'
            ),
            'validExtension' => array( // zelf gekozen naam van de regel
                'rule' => array('extension', array('jpg', 'jpeg', 'png', 
'gif')),
                'on' => 'create',
                'message' => 'Photo has to contain a valid extension (jpg, 
jpeg, png or gif).'
            ),
            'validExtension' => array( // zelf gekozen naam van de regel
                'rule' => array('extension', array('jpg', 'jpeg', 'png', 
'gif')),
                'allowEmpty' => true,
                'on' => 'update',
                'message' => 'Photo has to contain a valid extension (jpg, 
jpeg, png or gif).'
            )
        )
    );
    
    public function afterValidate()
    {
        $filename = $this->data['Outlet']['photo']['name'];
        
        if (!empty($filename)) {
            move_uploaded_file($this->data['Outlet']['photo']['tmp_name'], 
WWW_ROOT . 'img' . DS . 'outlets' . DS . $filename);
            
            $this->set('photo', $filename);
        } else {
            unset($this->data['Outlet']['photo']);
        }
    }
}


The "add" validation works fine for me. But strange as it is, when I edit a 
record, I keep getting the error message "Photo has to contain a valid 
extension (jpg, jpeg, png or gif)."
Somebody who can help me out of this? ;)

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Reply via email to