3

saveAll で複数のモデルを検証しようとしています。最初のモデルの検証はトリガーされていますが、関連するモデルに関しては何も起こっていないようです。exit を配置して 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->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

saveAll を使用して、乗算モデルの検証結果を取得できます。

$validate = $this->Model->saveAll($this->request->data, array('validate' => 'only'));
于 2014-10-14T17:00:42.620 に答える
1

オプション deep を追加してみてください:

$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 に答える