3

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

联系方式型号:

<?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'
            )
        )
    );
}

项目位置模型:

<?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->request->data :

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(

    );  
}
4

2 回答 2

1

You can get validation result for multiply models using saveAll:

$validate = $this->Model->saveAll($this->request->data, array('validate' => 'only'));
于 2014-10-14T17:00:42.620 回答
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

于 2013-02-28T10:18:18.657 回答