Passwords must match rule

I added the following to my UsersTable.php but it doesn’t show the message when the passwords don’t match

$validator -> sameAs('password_confirm','password','Passwords not equal.');

password_confirm is not a field in the database. I need to compare the two passwords and show the error message if they don’t match but it’s not working. I googled the problem but all of the solutions were for earlier versions of cakephp. I’m using cakephp version 4. I think the problem is because password_confirm isn’t a field but I don’t want to store the password_confirm in the database.

I removed the validator for password_confirm from UsersTable. I removed this:

    /*
            $validator
                ->scalar('password_confirm')
                ->maxLength('password_confirm', 255) 
                ->allowEmptyString('password_confirm');
    */

It’s working now. It’s not showing the error message from UsersTable but it’s showing the error message from the controller.