我想验证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说的那样,你不需要将字段名称传递给自定义验证规则。这有点奇怪,但是字段名默认传入第一个参数中,后面是在验证函数名后面指定的其他参数。 –
我试图做你告诉我的,但仍然没有成功:/我更新了我试过的一些更改的问题。 –