Validating fields based on the values of other fields can be hard to understand when you are new to CakePHP. A simple example of this can be comparing two values, like e-mail addresses or passwords. It is actually pretty easy because in the custom validation method you have access to $this->data
.
So here is how it goes. This will also invalidate the value compared with, so you will have to call this validation rule only once.
public function compare($value, $options = array(), $rule = array()) { $valid = current($value) == $this->data[$this->alias][$options[0]]; if (!$valid) { $this->invalidate($options[0], $rule['message']); } return $valid; }
In $validate
it will look like this:
public $validate = array( 'email' => array( /* 'email' => array( 'rule' => 'email', 'message' => 'This must be a valid e-mail address.', 'last' => true ), */ 'compare' => array( 'rule' => array('compare', array('email_confirm')), 'message' => "These e-mail addresses don't match." ), /* 'maxlength' => array( 'rule' => array('maxLength', 100), 'message' => "The e-mail address is too long." ), 'isunique' => array( 'rule' => 'isUnique', 'message' => 'This e-mail address is already in our database.' ) */ ) );