Reputation: 506
I'm trying to do this With last 2.x CakePHP version, I'm trying to do a upload multiple file form with validations but not working...
DocsController.php (action add)
if ($this->request->is('post')) {
if ($this->Doc->saveMany($this->request->data, array('deep' => true))) {
$this->Session->setFlash(__('Ok'));
} else {
debug($this->Doc->validationErrors); die();
$this->Session->setFlash(__('Error'));
}
$this->redirect($this->referer());
}
add.ctp
<?php
echo $this->Form->create('Doc', array('type' => 'file'));
echo $this->Form->input('files.', array(
'label' => __("New document",true), 'type' => 'file',
'div' => 'input text no',
'multiple' => 'multiple',
'required' => true,
));
echo $this->Form->end('Upload');
?>
Doc.php
class Doc extends AppModel {
public $belongsTo = array(
'Project' => array(
'className' => 'Project',
'foreignKey' => 'project_id',
)
);
public $validate = array(
'files' => array(
'rule' => array('extension', array('pdf')),
'required' => false,
'allowEmpty' => true,
'message' => 'Only pdf files'
),
);
}
Arrays
This error is OK:
Array
(
[Doc] => Array
(
[files] => Array
(
[0] => Array
(
[name] => images.jpeg
[type] => image/jpeg
[tmp_name] => /private/var/tmp/phpSqerRI
[error] => 0
[size] => 5740
)
)
)
)
/app/Controller/DocsController.php (line 112)
array(
'Doc' => array(
'files' => array(
(int) 0 => 'Only pdf files'
)
)
)
This is the problem:
Array
(
[Doc] => Array
(
[files] => Array
(
[0] => Array
(
[name] => 4_54718093804437603.pdf
[type] => application/pdf
[tmp_name] => /private/var/tmp/phpCfUUDx
[error] => 0
[size] => 1441232
)
)
)
)
/app/Controller/DocsController.php (line 112)
array(
'Doc' => array()
)
It's PDF, so OK but validation returns array
Why return array if no broke rules??
EDIT 1
Error is from saveMany, because checking validate says 'Ok'... but no more info atm.....
$this->Doc->set($this->request->data);
if ($this->Doc->validates()) {
// success
die("Ok");
} else {
// failed
$errors = $this->Doc->validationErrors;
die(print_r($errors));
}
Thanks in advance, sorry for my english.
Upvotes: 3
Views: 155
Reputation: 392
It is a problem with the format of your save for saveMany in cake2 :)
Array should be like 0 => Doc => array ('fieldname')
Upvotes: 2