I wonder if it is possible with CakePHP validation rules to validate a field depending on another.
I have been reading the documentation about custom validation rules but the $check param only contains the value of the current field to validate.
For example. I would like to define the verify_password field as required only if the new_password field is not empty. (in case
I could do it with Javascript anyway but i wonder if it is possible to do it directly with CakePHP.
When you validate data on a model, the data is already
set(). This means that you can access it on the model’s$dataproperty. The example below checks the field we’re validating to make sure it’s the same as some other field defined in the validation rules (such as a password confirm field).The validation rule would look something like this:
Our validation function then looks at the passed field’s data (confirm_password) and compares it against he one we defined in the rule (passed to
$compareFiled).This is a simple example, but you could do anything you want with
$this->data.The example in your post might look something like this:
And the rule: