user1052933
user1052933

Reputation: 191

CakePHP Validation Rule Error

I've go some validation functions written to check if the user's email exists in the system.

I am getting the following error

Notice (8): Undefined offset: 0 [CORE/cake/libs/model/model.php, line 1122]

This is the code which causes the error

'email' => array(
            'emailRule-1' => array(
                'rule' => 'email',
                'message' => 'email format is incorrect',
                'last' => true
            ),
            'emailRule-2' => array(
                'rule' => 'checkEmailExist',
                'message' => 'email already exists in the system'
            )
        ),

And rule2 seems to be responsible for the error, and here is the rule2:

function checkEmailExist($emailAddress, $user_id){
       $this->recursive = -1;
       if($user_id > 0){
           $user = $this->read(array('email'), $user_id);

           if($emailAddress == $user['User']['email'])
              return true;
       }


       $result = $this->find('count', array('conditions' => array('User.email' => $emailAddress)));
       return $result > 0 ? false : true;
    }

Upvotes: 0

Views: 641

Answers (2)

mark
mark

Reputation: 21743

Did you try to debug what $emailAddress contains? I bet this is an array^^

function checkEmailExist($emailAddress, $user_id){
   $this->recursive = -1;
   $email = array_shift(emailAddress);
   ...

you need to get the child element first

so remember: always a good idea to use debug() or pr() to debug your variables first.

Upvotes: 0

wrdevos
wrdevos

Reputation: 2069

Why not do it like this?

public $validate = array(
    'email' => array(
        'rule' => array('email', 'isUnique')
    )
);

You might want to split it up into two separate rules to apply your own error messages, but this should work just fine.

Upvotes: 1

Related Questions