Saturday, February 28, 2015

Re: File upload validation

Sam:

Based on your code the value for $this->data['Outlet']['photo'] is an array instead of a string field indicated by $validate and "$this->set('photo', $filename)".  This suggests that the type for  $this->data['Outlet']['photo'] is transitioning from an array to a string.  You would have to make sure that $this->data['Outlet']['photo'] contains a string value when validate() is executed.  Is it possible that your afterValidate function is supposed to be beforeValidate?

--Charles

On Sat, Feb 28, 2015 at 2:42 PM, Sam Clauw <sam.clauw@gmail.com> wrote:
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 cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

--
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 cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: