我正在开发一个表单,用户可以填写手机号码。或电话号码或两者。所以我想添加验证来做到这一点。
cakephp版本:2.5
'mobile' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message'=> 'Please enter mobile'
)
'telephone' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message'=> 'Please enter telephone'
)
我想在模型中将两者与 OR 条件组合。
最佳答案
首先从这里获取条件行为: Conditional Validation Behavior
将行为添加到您的模型中:
class Something extends AppModel
{
public $actsAs = array('ConditionalValidation');
}
按模型定义验证规则:
class Something extends AppModel
{
public $actsAs = array('ConditionalValidation');
public $validate = array(
'telephone' => array('isActive' => array(
'rule' => 'notEmpty',
'if' => array('mobile',''),//activate when you don't have mobile
),
'mobile' => array('isActive' => array(
'rule' => 'notEmpty',
'if' => array('telephone',''),//activate when you don't have mobile
),
);
);
}
必须找出两个字段存在的规则是什么
关于CakePHP 在模型中使用 OR 条件进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30749218/