php – saveMany验证无法正常工作

我正在尝试使用最后的2.x Cake
PHP版本,我正在尝试使用验证上传多个文件表单但不能正常工作…

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,所以没问题,但验证会返回数组

如果没有破坏规则,为什么返回数组

编辑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 =>数组(‘fieldname’)

点赞