saveMany验证无法正常工作

时间:2015-07-02 19:59:46

标签: php cakephp

我正在尝试使用最后的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,所以确定但验证返回数组

为什么在没有违反规则的情况下返回数组?

编辑1

错误来自saveMany,因为check validate显示'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));
}

提前致谢,对不起我的英语。

1 个答案:

答案 0 :(得分:2)

在cake2中保存saveMany的格式存在问题:)

数组应该像0 =&gt; Doc =&gt; array('fieldname')