0

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;
    }

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.