Manish Prajapati
Manish Prajapati

Reputation: 1641

CakePHP validation with OR condition in Model

i am developing a form where a user can be filled either mobile no. OR telephone no. or both. so i want to add validation to do this.

cakephp version : 2.5

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

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

i want to combination of both with OR condition in Model.

Upvotes: 2

Views: 521

Answers (1)

Andrewboy
Andrewboy

Reputation: 364

First Get the Conditional Behaviour from here: Conditional Validation Behavior

Add Behavior to your model:

class Something extends AppModel
{
    public $actsAs = array('ConditionalValidation');
}   

Defining your validation rule by Model:

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
            ),



        );
    );
}   

Have to find out whats the rule for both fields exist

Upvotes: 3

Related Questions