Reputation: 333
My validation for my image upload is always firing the message related with the allowed size for the file to be uploaded, after I copied the code to the top of the validation array where previouslly was the is_valid
rule, which was being always triggered as well, even when the file size is lower than the limit and even when the file is uploaded successfully. My is_unique
rule is working as expected but the rest seem to be always triggered even when the files obey the rules .What is triggering this behaviour?
I am using CakePHP 2.4.4.
Controller
public function admin_upload_image(){
$this->set('title_for_layout', 'Inserir Fotografias');
if(!$this->Session->check('User')) {
$this->Session->setFlash('Está a aceder a uma zona restrita. Por favor faça Login.');
$this->redirect(array(
'controller' => 'users',
'action' => 'login'));
}
$this->layout = 'admin_index';
if($this->request->is('post') || $this->request->is('put')) {
/* $file = $this->request->data['gallery_images']['path']['name'];*/
//debug($this->request->data['gallery_images']['path']['name']);
//die;
$file = array(
'GalleryImage' => array(
'path' => $this->request->data['gallery_images']['path']['name']
)
);
move_uploaded_file($this->data['gallery_images']['path']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/html/PushUp/app/webroot/img/gallery/' . $this->data['gallery_images']['path']['name']);
$this->loadModel('GalleryImage');
$this->GalleryImage->create();
//debug($file);
//die;
if($this->GalleryImage->save($file)){
$validationErrors = $this->GalleryImage->invalidFields();
$this->Session->setFlash($validationErrors['path']); // named key of the rule
$this->Session->setFlash('Fotografia guardada com sucesso.', 'default', array('class'=>'alert alert-success'));
}
//else{
//debug($this->GalleryImage->invalidFields());
//die;
//$error = $this->Notification->validationErrors;
//$this->set('error', $error);
//$this->Session->setFlash(__($error), 'Flash/warning');
//}
}
}
Model
<?php
App::uses('AppModel', 'Model');
class GalleryImage extends AppModel{
public $displayField ='path';
public $useTable = 'gallery_images';
//public $actsAs = array('MultipleDisplayFields' => array('fields' => array('path', 'id')));
var $name = 'GalleryImage';
var $validate= array(
'path' => array(
'size' => array(
'rule' => array('fileSize','<=','1.5MB'),
'message' => 'O ficheiro deve ter um tamanho igual ou inferior a 1.5MB.',
//'last' => true,
'required'=> true),
'is_valid' => array(
'rule' => 'fileSelected',
'message' => 'Seleccione uma fotografia por favor.',
//'last' => true,
'required'=> true),
'extension' => array(
'rule' => array('extension', array('gif','jpeg','png','jpg')),
'message'=> 'A imagem deve estar num formato gif, jpeg, png ou jpg.',
//'last' => true,
'required'=> true),
'is_unique' => array(
'rule' => 'isUnique',
'message' => 'Uma fotografia com este nome já existe.',
'required'=> true
)
)
);
/*public function isUploadedFile($params) {
$val = array_shift($params);
if ((isset($val['error']) && $val['error'] == 0) || (!empty( $val['tmp_name']) && $val['tmp_name'] != 'none')) {
return is_uploaded_file($val['tmp_name']);
}
return false;
}*/
public function fileSelected($file) {
if(is_array($file) && array_key_exists('path', $file) && !empty($file['path'])) {
// Seems like a file was set
return true;
}
// No file set, doesn't validate!
return false;
}
}
?>
View
<style>
.alert-warning{
width:100%;
}
.error-message{
}
.col-lg-4{
width:100%;
}
</style>
<h2>Apagar Fotografia</h2>
<?php echo $this->Session->flash();?>
<br>
<table border="1" bordercolor="#e2e2e2" width="720" style="word-wrap: break-word" cellpadding="5px" class="">
<tr>
<?php
$i=0;
foreach( $gallery_images as $gallery_image ):?>
<?php
echo "<td style=text-align: justify>";
//echo $gallery_image['GalleryImage']['path'];
echo $this->Form->postLink('Apagar', array('controller'=>'Galleries', 'action'=>'admin_del_image', $gallery_image['GalleryImage']['id']/*,'prefix'=>'admin'*/), array('class'=>'foto_del btn btn-danger', 'title'=>'Apagar Fotografia'), __('Tem a certeza que quer apagar esta Fotografia?'));
echo "</td>";
echo "<td>";
//$src3 =$this->webroot. 'img/gallery/' .$gallery_image['GalleryImage']['path'];
echo $this->Html->image('gallery/' . $gallery_image['GalleryImage']['path'] , array('width' => '200px', 'height' => '133px', 'alt' => $gallery_image['GalleryImage']['path'] ));
echo "</td>";
$i++;
if($i==4){
echo "</tr><tr>";
$i=0;
}
?>
<?php endforeach ?>
</tr>
</table>
Upvotes: 2
Views: 227
Reputation: 122
You are passing the $file variable to be save $this->GalleryImage->save($file)
and you are setting this variable value manually
$file = array(
'GalleryImage' => array(
'path' => $this->request->data['gallery_images']['path']['name']
)
);
so you are not passing file information to your model. you have to pass $this->request->data['gallery_images']['path'] to your save method
Upvotes: 3