saveMany 验证无法正常工作

Posted

技术标签:

【中文标题】saveMany 验证无法正常工作【英文标题】:saveMany validation not working correctly 【发布时间】:2015-07-02 19:59:45 【问题描述】:

我正在尝试使用最新的 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,因为检查验证说“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】:

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

数组应该像 0 => Doc => array ('fieldname')

【讨论】:

以上是关于saveMany 验证无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

验证不适用于 saveMany

CakePHP SaveMany 不保存

1000hz Bootstrap 验证无法正常工作

从 POST 操作验证模态后无法正常工作

正则表达式验证属性无法正常工作

select2 和 jquery 验证无法正常工作