Reputation: 333
I am a newbie. I have tried many solutions on other posts but I just can't get it right. The mesages on my validation simply don't show up, but the value of the file input field gets null if the size validation does not pass, but it seems that the extension valdiation is not working as well. Why are no messages showing up and the extension validation not working? 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'];*/
$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(__('Evento guardado com sucesso.'));
}
//else{
//$error = $this->Notification->validationErrors;
//$this->set('error', $error);
//$this->Session->setFlash(__($error), 'Flash/warning');
//}
}
}
View
<h2>Adicionar Fotografia</h2>
<?php
echo "<br>";
echo $this->Form->create('GalleryImage',array('type'=>'file'));
echo $this->Form->file('gallery_images.path');
echo "<br>";
echo $this->Form->submit(__('Guardar'), array('class' => 'btn btn-success','formnovalidate' => false)) ;
echo $this->Form->end();
/*if ($this->Form->isFieldError('path')) {
echo $this->Form->error('path');
}*/
?>
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(
'is_valid' => array(
'rule' => 'notEmpty',
'message' => 'Seleccione uma fotografia por favor.',
'last' => true),
'size' => array(
'rule' => array('fileSize','<=','1.5MB'),
'message' => 'O ficheiro deve ter um tamanho igual ou inferior a 1.5MB.',
'last' => 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)
)
);
}
?>
debug($file)
\app\Controller\GalleriesController.php (line 58)
array(
'GalleryImage' => array(
'path' => '1604710_722861904399871_963210258_n.jpg'
)
)
pr($this->GalleryImage->invalidFields());
Notice (8): Undefined index: gallery_images [APP\Controller\GalleriesController.php, line 50]
Notice (8): Undefined index: gallery_images [APP\Controller\GalleriesController.php, line 53]
Notice (8): Undefined index: gallery_images [APP\Controller\GalleriesController.php, line 53]
Array
(
[path] => Array
(
[0] => Seleccione uma fotografia por favor.
[1] => Seleccione uma fotografia por favor.
)
)
Upvotes: 1
Views: 133
Reputation: 66218
"building block" functions such as checkbox, radio, select and file are just the input. The normal way to generate forms is to use the input (or inputs) method:
echo $this->Form->input('path', array('type' => 'file'));
Alternatively, you can render the validation errors explicitly:
echo $this->Form->file('path');
echo $this->Form->error('path');
Upvotes: 2