使用模型

时间:2015-06-10 06:59:15

标签: cakephp

我正在开发一个表单,用户可以填写移动号码。或电话号码。或两者。所以我想添加验证来做到这一点。

cakephp版本:2.5

'mobile' => array(
        'notEmpty' => array(
                'rule' => 'notEmpty',
                'message'=> 'Please enter mobile'
        )

'telephone' => array(
        'notEmpty' => array(
                'rule' => 'notEmpty',
                'message'=> 'Please enter telephone'
        )

我希望将两者与模型中的OR条件组合。

1 个答案:

答案 0 :(得分:3)

首先从此处获取条件行为: 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
            ),



        );
    );
}   

必须找出两个字段的规则是否存在

相关问题