As a rule, you can access all the data in a custom validation rule using the $context argument, where it is stored in the data key, i.e. $context['data']['confirm_password'] , which can then be compared with the current value of the field.
$validator->add('password', 'passwordsEqual', [ 'rule' => function ($value, $context) { return isset($context['data']['confirm_password']) && $context['data']['confirm_password'] === $value; } ]);
Recently, the compareWith validation rule has been introduced, which does just that.
https://github.com/cakephp/cakephp/pull/5813
$validator->add('password', [ 'compare' => [ 'rule' => ['compareWith', 'confirm_password'] ] ]);
ndm
source share