我想驗證saveAll上的多個模型。第一個模型中的驗證正在被觸發,但是當涉及到相關模型時,似乎沒有任何事情發生。我甚至嘗試通過放置exit來檢查beforeValidate()和beforeSave()方法是否被調用;在他們中,但代碼繼續執行正常。CakePHP 2.2 saveAll多個模型驗證
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.'
)
)
);
}
這是$這個 - >請求 - 我試圖挽救>數據:
Array
(
[ContactDetails] => Array
(
[id] => 1
[name] => PreeoStudios
[address1] => 4, Stivala Street
[address2] => Mosta, MST 3205
[address3] => Malta
[telephone] => 34562737
[email] => [email protected]
[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(
);
}
你能也張貼代碼爲您定義的'checkMap'驗證方法?你也可以嘗試使用'saveAssociated()'來進行測試。 另外你爲什麼要將字符串「lat」傳遞給驗證? –
我會嘗試爲ProjectLocation保存一行,看看它是否得到驗證。像Borislav Sabev說的那樣,你不需要將字段名稱傳遞給自定義驗證規則。這有點奇怪,但是字段名默認傳入第一個參數中,後面是在驗證函數名後面指定的其他參數。 –
我試圖做你告訴我的,但仍然沒有成功:/我更新了我試過的一些更改的問題。 –