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 9097465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:07:30+00:00 2026-06-17T00:07:30+00:00

I wonder if it is possible with CakePHP validation rules to validate a field

  • 0

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.

  • 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-06-17T00:07:31+00:00Added an answer on June 17, 2026 at 12:07 am

    When you validate data on a model, the data is already set(). This means that you can access it on the model’s $data property. 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:

    var $validate = array(
        'password' => array(            
            'minLength' => array(
                'rule' => array('minLength', 6),
                'message' => 'Your password must be at least 6 characters long.'
            ),
            'notempty' => array(
                'rule' => 'notEmpty',
                'message' => 'Please fill in the required field.'
            )
        ),
        'confirm_password' => array(
            'identical' => array(
                'rule' => array('identicalFieldValues', 'password'),
                'message' => 'Password confirmation does not match password.'
            )
        )
    );
    

    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).

    function identicalFieldValues(&$data, $compareField) {
        // $data array is passed using the form field name as the key
        // so let's just get the field name to compare
        $value = array_values($data);
        $comparewithvalue = $value[0];
        return ($this->data[$this->name][$compareField] == $comparewithvalue);
    }
    

    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:

    function requireNotEmpty(&$data, $shouldNotBeEmpty) {
        return !empty($this->data[$this->name][$shouldNotBeEmpty]);
    }
    

    And the rule:

    var $validate = array(
      'verify_password' => array(
        'rule' => array('requireNotEmpty', 'password')
      )
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this question I started to wonder: is it possible to have a
I wonder is it possible to get cookies under another domain rather than my
I wonder if is possible to have inner shadow effect on only one corner
I'm working under ASP.NET and I wonder if is possible to display a custom
I wonder if it possible to not have jython automagicaly transform java objects to
I wonder if its possible to have a watermark on hotlinked images on an
I wonder if its possible to use a custom font for my form labels
I wonder is it possible to keep the user input inside form field after
I have an APK for test and I wonder if is possible to know
As the title says, I wonder if its possible to have more than one

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.