Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3286358
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:22:15+00:00 2026-05-17T20:22:15+00:00

I have a custom validation rule to check if two passwords entered are the

  • 0

I have a custom validation rule to check if two passwords entered are the same, and if they arent I wish to have a message that says “Passwords do not match”.

The rule works, however, when the passwords don’t match it simply displays the normal error message, what’s going on?

var $validate=array(
        'passwd2' => array('rule' => 'alphanumeric',
                        'rule' => 'confirmPassword',
                        'required' => true,
                        'allowEmpty'=>false));

function confirmPassword($data)
{
    $valid = false;
    if ( Security::hash(Configure::read('Security.salt') .$data['passwd2']) == $this->data['User']['passwd'])
    {
        $valid = true;
        $this->invalidate('passwd2', 'Passwords do not match');
    }
    return $valid;
}

It says “This field cannot be left blank”

EDIT:

The strange thing is, if I leave one of the password fields blank, both error messages say “This field cannot be left blank”

However, if I put something in both, then it correctly says “Passwords do not match”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:22:15+00:00Added an answer on May 17, 2026 at 8:22 pm

    I think you made it too complex. Here is how I do it:

        // In the model
        public $validate = array(
            'password' => array(
                'minLength' => array(
                    'rule' => array('minLength', '8')
                ),
                'notEmpty' => array(
                    'rule' => 'notEmpty',
                    'required' => true
                )
            ),
            'confirm_password' => array(
                'minLength' => array(
                    'rule' => array('minLength', '8'),
                    'required' => true
                ),
                'notEmpty' => array(
                    'rule' => 'notEmpty'
                ),
                'comparePasswords' => array(
                    'rule' => 'comparePasswords' // Protected function below
                ),
            )
        );
        protected function comparePasswords($field = null){
            return (Security::hash($field['confirm_password'], null, true) === $this->data['User']['password']);
        }
    
    // In the view
    echo $form->input('confirm_password', array(
        'label' => __('Password', true),
        'type' => 'password',
        'error' => array(
            'comparePasswords' => __('Typed passwords did not match.', true),
            'minLength' => __('The password should be at least 8 characters long.', true),
            'notEmpty' => __('The password must not be empty.', true)
        )
    ));
    echo $form->input('password', array(
        'label' => __('Repeat Password', true)
    ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom validation rule on a model that says the user can't
I have custom validation rule: public function customRule($check) { } Inside this rule I
I have created a custom validation rule (uniqueUserName). The problem I'm having is that
I have two custom validation rules (I have tested they work correctly): class PasswordResetKey
I have this custom validation attribute for validating a collection. I need to adapt
I have a custom validation attribute derived from action filter attribute. Currently the attribute
I have a custom validation in the enterprise validation block. The DoValidate method is
i have a validation class which needs improving. If I require some custom validation
I'm trying to setup custom validation for a checkbox. I have 7 checkboxes each
I have a problem with Hibernate Validator 3.1.0.GA not looking up custom validation messages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.