2

I have the below setup of validation rules. For some reason, 'on' => 'create' block doesn't work. The conditions to be implemented are standard create / modify regarding email. Also, in edit section, I'm getting the error from 'on' => 'create' block.

How to validate the email? I'm using CakePHP v 2.6.1.

public $validate = array(
    'email' => array(
        'required' => array(
            'rule' => array('email'),
            'message' => 'Kindly provide your email for verification.'
        ),
        'maxLength' => array(
            'rule' => array('maxLength', 255),
            'message' => 'Email cannot be more than 255 characters.'
        ),
        'editunique' => array(
            'rule' => array('editunique'),
            'message' => 'Provided Email address already exists.',
            'on' => 'update'
        ),
        'unique' => array(
            'rule' => 'isUnique',
            'message' => 'Provided Email already exists.',
            'on' => 'create'
        )
    )
);


public function editunique($email) {
    // email should be one and of the logged in user only.
    if ($this->find('count', array(
        'conditions' => array(
            $this->alias . '.id <>' => $this->data[$this->alias]['id'],
            $this->alias . '.email' => $email
        )
    )) > 1) {
        return false;
    }

}

Also, I'm not getting the $this->data[$this->alias]['id'] value.

My Controller has the following section:

if ($this->Client->hasAny(array('Client.id' => base64_decode(trim($this->request->query['client_id']))))){
            if ( $this->request->is('ajax') && $this->request->is('post') ){
                $this->Client->create();
                $this->Client->id = base64_decode(trim($this->request->query['client_id']));
                $this->Client->set($this->request->data);
                // validate
                if($this->Client->validates()) {
                    // save the data after validation
                    if($this->Client->save($this->request->data)){
                     }
                 }
             }
        }
drmonkeyninja
  • 8,490
  • 4
  • 31
  • 59
Sp0T
  • 284
  • 8
  • 23
  • ok, to get `$this->data[$this->alias]['id']` value, change to `$this->id`. So getting the set id now but problem with create persists. – Sp0T Feb 16 '16 at 12:29
  • What are you trying to do with your custom validation rule? That looks to replicate Cake's own `isUnique` validation rule. – drmonkeyninja Feb 16 '16 at 12:32
  • yes that is for uniqueness but on update of profile details. Actually I took the function from this - http://stackoverflow.com/a/20203538/3007408. While that answer tells to use in before save, i'm using it during validation. – Sp0T Feb 16 '16 at 12:42
  • Then why not remove the `editunique` rule and drop the `on` condition of your `unique` rule as unless I've missed something that would do the same thing with much less code. Cake's `isUnique` takes into account the current record's value so won't complain of duplicates on updating if the email is only used on the current data. – drmonkeyninja Feb 16 '16 at 12:47
  • 1
    oh, didn't knew that. Although i've not check the registration part but seems to work for the edit. Thanks. – Sp0T Feb 16 '16 at 12:51
  • That's great. I've added this as an answer to explain things better. Perhaps you could accept it as the correct answer to help others facing a similar problem? :-) – drmonkeyninja Feb 16 '16 at 12:55

2 Answers2

3

I think you are misunderstanding what Cake's isUnique rule checks for and as a result over complicating things. Cake defines isUnique as:-

The data for the field must be unique, it cannot be used by any other rows

When it checks if a value is unique it is smart enough to exclude existing data of the current row (which appears to be what you are attempting to do with your editunique rule).

So you just need your validation rules to look like:-

public $validate = array(
    'email' => array(
        'required' => array(
            'rule' => array('email'),
            'message' => 'Kindly provide your email for verification.'
        ),
        'maxLength' => array(
            'rule' => array('maxLength', 255),
            'message' => 'Email cannot be more than 255 characters.'
        ),
        'unique' => array(
            'rule' => 'isUnique',
            'message' => 'Provided Email already exists.'
        )
    )
);

This removes the editunique rule and drops the on condition of your unique rule.

drmonkeyninja
  • 8,490
  • 4
  • 31
  • 59
0

As of cakephp 3.0 in the entities table it should look something like this

   namespace App\Model\Table;

   public function validationDefault($validator)
   {
    $validator
        ->email('email')
        ->add('email', 'email', [
            'rule' => [$this, 'isUnique'],
            'message' => __('Email already registered')
        ])
        ->requirePresence('email', 'create')
        ->notEmpty('email', 'Email is Required', function( $context ){
            if(isset($context['data']['role_id']) && $context['data']['role_id'] != 4){
                return true;
            }
            return false;
        });
    return $validator;
}
}


function isUnique($email){
    $user = $this->find('all')
        ->where([
                'Users.email' => $email,
          ])
        ->first();
        if($user){
            return false;
        }
        return true;
}