我想知道是否可以使用类似inList()
的方法验证字段,但使用其他模型中其他字段的值:
例如,我想使用来自type_id
模型的Member
字段中的值来验证id
模型的MemberType
字段。
Member
型号
<?php
class Member extends AppModel
{
public $belongsTo = array(
"MemberType" => array(
"className" => "MemberType"
)
);
public $validate = array(
"type_id" => array(
"validType" => array(
'rule' => array('inList', array('A', 'B')),
'message' => 'Choose an existing type'
)
)
);
}
MemberType
模型
<?php
class MemberType extends AppModel
{
}
答案 0 :(得分:1)
尝试使用自定义验证方法验证type_id
字段:
<?php
class Member extends AppModel
{
public $belongsTo = array(
"MemberType" => array(
"className" => "MemberType"
)
);
public $validate = array(
"type_id" => array(
"validType" => array(
'rule' => array('validMemberType'),
'message' => 'Choose an existing type'
)
)
);
public function validMemberType($check){
$types = array_keys($this->MemberType->find("list"));
return in_array($check["type_id"], $types);
}
}