I'm sorry.. I replied quickly via cell phone. A better response is as follows.Try the following code:<?php
class Outlet extends CoasterCmsAppModel
{
public $validate = array(
'name' => array(
'rule' => 'notEmpty', // verplicht
'message' => 'Nameis 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.'
)
),
'photoTemp' => array(
'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 beforeValidate()
{
$unset_photo = false;
if ( is_array( $this->data['Outlet']['photo'] ) ) {
$filename = false;
$tmp_filename = false;
$unset_photo = true;
$upload_path = WWW_ROOT .'img' . DS . 'outlets' . DS;
if ( isset( $this->data['Outlet']['photo']['name'] ) ) {
$filename = trim($this->data['Outlet']['photo']['name']);
}
if ( isset( $this->data['Outlet']['photo']['tmp_name'] ) ) {
$tmp_filename = trim($this->data['Outlet']['photo']['tmp_name']);
}
if ( ! empty( $tmp_filename ) && ! empty( $filename ) ) {
// Move the uploaded file to the new directory
if(!move_uploaded_file($tmp_filename, $upload_path basename($filename))) {
/*
* Handle upload error and return FALSE; e.g.
* $this->validationErrors['Outlet']['photo'] = 'Failed to upload file ' . $filename;
*/
} else {
$this->data['Outlet']['photo'] = $filename; // SUCCESS
}
} else if ( ! empty( $this->data['Outlet']['photo'] ) ) ) {
/*
* Handle Invalid input error and return FALSE; e.g.
*
* $this->validationErrors['Outlet']['photo'] = 'Invalid input: photo tmp_name required';
* $this->validationErrors['Outlet']['photo'] = 'Invalid input: photo name required';
*/
} else {
/*
* Silently unset and ignore an empty array.
*/
unset($this->data['Outlet']['photo']);
}
}
}
?>
On Sat, Feb 28, 2015 at 6:24 PM, Charles Beasley <carmelchas@gmail.com> wrote:I think you should change your function to beforeValidate
On Feb 28, 2015 5:54 PM, "Sam Clauw" <sam.clauw@gmail.com> wrote:Okay Charles , that makes sense so I changed my code:--
<?php
class Outlet extends CoasterCmsAppModel
{
public $validate = array(
'name' => array(
'rule' => 'notEmpty', // verplicht
'message' => 'Nameis 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.'
)
),
'photoTemp' => array(
'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 beforeValidate()
{
$this->data['Outlet']['photoTemp'] = $this->data['Outlet']['photo'];
$this->data['Outlet']['photo'] = $this->data['Outlet']['photoTemp']['name'];
}
public function afterValidate()
{
$filename = $this->data['Outlet']['photo'];
if (!empty($filename)) {
move_uploaded_file($this->data['Outlet']['photoTemp']['tmp_name'], WWW_ROOT . 'img' . DS . 'outlets' . DS . $filename);
} else {
unset($this->data['Outlet']['photo']);
}
unset($this->data['Outlet']['photoTemp']);
}
}
However, I still get the error message that I should upload a file with a correct extension. It's just like 'allowEmpty' => true" isn't working at all.I quess in my case it's checking the value in $this->data['Outlet']['photoTemp']['type'] so what could possibly be the problem?
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.