Interview Questions

What is wrong with the below validation rule?

CakePHP Interview Questions and Answers


(Continued from previous question...)

What is wrong with the below validation rule?

'email' => array(
'rule' => array(
'rule' => 'notEmpty',
'message' => 'Please Enter Email address.'
),
'rule' => array(
'rule' => 'email',
'message' => 'Entered Email address is invalid.'
)
)


The problem is the first rule notEmpty will never be called because email rule will overwrite it.While using multiple validation rules for the same field you must keep the rule key "unique". In this case if we want to use multiple rules then, we can simple change the rule key names like,

'email' => array(
'rule1' => array(
'rule' => 'notEmpty',
'message' => 'Please Enter Email address.'
),
'rule2' => array(
'rule' => 'email',
'message' => 'Entered Email address is invalid.'
)
)

(Continued on next question...)

Other Interview Questions