CakePHP 2.2 saveAll多个模型验证

时间:2012-11-24 15:12:43

标签: cakephp cakephp-2.2

我正在尝试在saveAll上验证多个模型。第一个模型中的验证被触发,但是当涉及相关模型时,似乎没有发生任何事情。我甚至尝试通过放置一个退出来检查beforeValidate()和beforeSave()方法是否被调用;在他们中但代码继续正常执行。

ContactDetails型号:

<?php
class ContactDetails extends ContactAppModel {  
    public $actsAs = array("MapValidate");
    public $hasMany = array(
        'ProjectLocation' => array(
            'className'     => 'ProjectLocation',
            'foreignKey'    => 'project_id'
        )
    );

    public $validate = array(
        'name' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Contact name is required'
            )
        ),
        'address1' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Contact address 1 is required'
            )
        ),
        'email' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Contact email is required'
            ),
            'email' => array(
                'rule' => array('email'),
                'message' => 'Contact email format is not valid'
            )
        )
    );
}

ProjectLocation模型:

<?php
class ProjectLocation extends ContactAppModel {
    public $actsAs = array("MapValidate");
    public $belongsTo = array(
        "ContactDetails" => array(
            "className" => "ContactDetails",
            "foreignKey" => "project_id"
        );
    );

    public $validate = array(
        'lat' => array(
           'checkLocation' => array(
                'rule'    => array('checkMap', 'lat'),
                'message' => 'One or more positions on the map are invalid.'
            )
        )
    );  
}

这是我要保存的$ this-&gt; request-&gt;数据:

Array
(
    [ContactDetails] => Array
        (
            [id] => 1
            [name] => PreeoStudios
            [address1] => 4, Stivala Street
            [address2] => Mosta, MST 3205
            [address3] => Malta
            [telephone] => 34562737
            [email] => info@preeostudios.com
            [fax] => N/A
            [skype] => N/A
        )

    [ProjectLocation] => Array
        (
            [0] => Array
                (
                    [lat] => 35.886277456343024
                    [lon] => 14.428907312499973
                )

            [1] => Array
                (
                    [lat] => 35.886277456343024
                    [lon] => 14.528907312499973
                )

        )

)

saveAll调用:

$this->ContactDetails->saveAll($this->request->data, array('validate' => 'first'))

修改

我还尝试从相关模型中删除验证规则,并在beforeSave函数中放置一个退出...代码只是一直执行

<?php
class ProjectLocation extends ContactAppModel {
    public $actsAs = array("MapValidate");
    public $belongsTo = array(
        "ContactDetails" => array(
            "className" => "ContactDetails",
            "foreignKey" => "project_id"
        );
    );

    public function beforeSave(){
        exit;
    }

    public $validate = array(

    );  
}

2 个答案:

答案 0 :(得分:1)

尝试添加深度选项:

$this->ContactDetails->saveAll($this->request->data, array('validate' => 'first', 'deep' => true))

来自:http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array

答案 1 :(得分:1)

您可以使用saveAll获取多个模型的验证结果:

$validate = $this->Model->saveAll($this->request->data, array('validate' => 'only'));