saveMany 验证无法正常工作
saveMany validation not working correctly
我正在尝试使用最新的 2.x CakePHP 版本,我正在尝试通过验证上传多个文件表单,但无法正常工作...
DocsController.php(动作添加)
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'
),
);
}
数组
这个错误是可以的:
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'
)
)
)
问题是这样的:
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()
)
它是 PDF,很好,但是验证 returns 数组
如果没有违反规则,为什么要 return 数组??
编辑 1
错误来自 saveMany,因为检查验证显示 'Ok'... 但没有更多信息 atm.....
$this->Doc->set($this->request->data);
if ($this->Doc->validates()) {
// success
die("Ok");
} else {
// failed
$errors = $this->Doc->validationErrors;
die(print_r($errors));
}
提前致谢,对不起我的英语。
cake2中saveMany的保存格式有问题:)
数组应该像 0 => Doc => array ('fieldname')
我正在尝试使用最新的 2.x CakePHP 版本,我正在尝试通过验证上传多个文件表单,但无法正常工作...
DocsController.php(动作添加)
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'
),
);
}
数组
这个错误是可以的:
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'
)
)
)
问题是这样的:
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()
)
它是 PDF,很好,但是验证 returns 数组
如果没有违反规则,为什么要 return 数组??
编辑 1
错误来自 saveMany,因为检查验证显示 'Ok'... 但没有更多信息 atm.....
$this->Doc->set($this->request->data);
if ($this->Doc->validates()) {
// success
die("Ok");
} else {
// failed
$errors = $this->Doc->validationErrors;
die(print_r($errors));
}
提前致谢,对不起我的英语。
cake2中saveMany的保存格式有问题:)
数组应该像 0 => Doc => array ('fieldname')