有没有办法验证数据(使用 CakePHP 的模型验证)以确保至少“a”或“b”有数据(不必同时有数据)。
问问题
1371 次
3 回答
4
在你的模型中,做这样的事情。执行保存操作时将调用该函数。
已编辑
public $validate = array(
'a' => array(
'customCheck' => array(
'rule' => 'abCheck',
'message' => 'You must enter data in a or b.'
)
),
'b' => array(
'customCheck' => array(
'rule' => 'abCheck',
'message' => 'You must enter data in a or b.'
)
)
);
//Function must be public for Validator to work
//Checks to see if either a or b properties are set and not empty
public function abCheck(){
if((isset($this->data['Model']['a']) && !empty($this->data['Model']['a'])) || (isset($this->data['Model']['b']) && !empty($this->data['Model']['b']))){
return true;
}
return false;
}
于 2011-06-02T01:00:23.637 回答
1
您可以通过“自定义验证”来验证这些条件。
看到这个:添加你自己的验证
于 2011-06-01T17:15:49.540 回答
0
试试这个:
public $validate = array(
'a' => array(
'customCheck' => array(
'rule' => array('abCheck', 1),
'message' => 'You must enter data in a or b.'
)
),
'b' => array(
'customCheck' => array(
'rule' => array('abCheck', 1),
'message' => 'You must enter data in a or b.'
)
)
);
//Function must be public for Validator to work
//Checks to see if either a or b properties are set and not empty
public function abCheck(){
if((isset($this->data['Model']['a']) && !empty($this->data['Model']['a'])) > || (isset($this->data['Model']['b']) && !empty($this->data['Model']['b']))){
return 1;
}
return -1;
}
于 2012-07-13T11:28:29.123 回答