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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:16:45+00:00 2026-06-07T17:16:45+00:00

What am I trying to do? I have three fields (1 hidden, an id)

  • 0

What am I trying to do?
I have three fields (1 hidden, an id) and the user must complete one of the other two in order to pass validation.
So the user should fail validation if both fields are empty, but pass if one is completed.

1 2 3
A 0 B True
A B 0 True
A 0 0 False

I’m using CakePHP v2.1.3 so have access to the new validation rule enhancements.

The problem
I can’t seem to find a reliable way to check both fields at the same time. I have so far tried looking at $this->data from the model and have found that validation is only passing a single instance of the data at a time. So there doesn’t seem to be a way to compare the fields.

What I have so far

/**
 * Custom validation to see if either of the two fields are set, if neither are, then we fail, if at least one is, we pass
 * @param array $check
 * @return boolean 
 */
public function checkAttributes($check){
    var_dump($check);
    var_dump($this->data);
    echo "<hr>";

    // Check for an id value picked from a list
    if(@is_numeric($check['attribute_value_id']) && isset($this->data['AdvertAttributeValue']['attribute_value_id'])){
        return true;
    }

    // Check for a date value selected
    if(@is_array($check['attribute_value_text']) && isset($this->data['AdvertAttributeValue']['attribute_value_text'])){
        return true;
    }

    // Check for a text value
    if(@is_string($check['attribute_value_text']) && isset($this->data['AdvertAttributeValue']['attribute_value_text'])){
        return true;
    }

    return false;
}

This doesn’t seem to do the trick as I think it can’t check $this->data because the instance of it doesn’t contain all the relevant fields.

The data
I should also mention that I am passing a large numeric array in. So these fields appear multiple times on the page, currently 12 dimensions. So accessing them directly through $this->data will be hard as they are not named dimensions, but are $this->data['Model'][<num>]['field'] = value


Validation

public $validate = array(
    'attribute_value_id'=>array(
        'notempty'=>array(
            'rule'=>'checkAttributes',
            'message'=>'Please select a value for your attribute',
            'required'=>true,
        ),
    ),
    'attribute_value_text'=>array(
        'notempty'=>array(
            'rule'=>'checkAttributes',
            'message'=>'You must enter text for this attribute',
            'required'=>true,
        ),
    )
);

Data dump
Here I’ll show the output of the var_dump() above. I have two validation rules in my Model, one for attribute_value_id and also one for attribute_value_text

// An id field selected from a list
array // $check
  'attribute_value_id' => string '1' (length=1)
array // $this->data
  'AdvertAttributeValue' => 
    array
      'attribute_value_id' => string '1' (length=1)
      'id' => string '' (length=0)

// A text field
// Validating first time around on the id field
array // $check
  'attribute_value_id' => string '' (length=0)
array // $this->data
  'AdvertAttributeValue' => 
    array
      'attribute_value_id' => string '' (length=0)
      'id' => string '' (length=0)
      'attribute_value_text' => string '50' (length=2)
// Validating second time around on the text field
array // $check
  'attribute_value_text' => string '50' (length=2)
array // $this->data
  'AdvertAttributeValue' => 
    array
      'attribute_value_id' => string '' (length=0)
      'id' => string '' (length=0)
      'attribute_value_text' => string '50' (length=2)  
// A date field
array // $check
  'attribute_value_id' => string '' (length=0)
array // $this->data
  'AdvertAttributeValue' => 
    array
      'attribute_value_id' => string '' (length=0)
      'id' => string '' (length=0)
      'attribute_value_text' => 
        array
          'month' => string '06' (length=2)
          'day' => string '28' (length=2)
          'year' => string '2012' (length=4)
  • 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-07T17:16:47+00:00Added an answer on June 7, 2026 at 5:16 pm

    The saveAll() method just calls saveMany() or saveAssociated() as appropriate. These methods will, by default, attempt to validate all of the data before saving any of it (via a call to validateMany()). However, as you can see in the source, that function validates each item individually, so the validation code won’t have access to other records.

    As I understand it, you need to cross-validate between multiple records before you save them. Although I’ve never done that, it sounds like a case for validation in the controller. You can make calls to Model::validate() or Model::validateAll() to ensure internal consistency of records. Then your controller can also implement whatever logic is necessary for your cross-record validation. Once that’s done, you can make the save call with validation disabled:

    $this->myModel->saveAll($this->request->data, array('validate'=>false));
    

    Note that before you do any of this, you’ll have to set your data to the model:

    $this->myModel->set($this->request->data);
    

    I realize this puts a lot of extra code in the controller that should ideally be in the model. I suppose it’s possible that it could be done via one of the model callbacks, but I’m not sure how.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three fields, namely Number1 Number2 Time I am trying to write a
I am trying to display three forms in one template. I have the forms
I am trying to get at least three words separated by two commas.I have
I'm trying to construct a HTML/PHP page with three hidden input fields. The values
I have three models that I'm trying to setup: Location/Venues, Categories, and Neighborhoods. A
I have three lists of lists, and I'm trying to write a generator function
I have been trying to get yui-css grid system to make a three column
I have the following three tables. I am trying to inter link (join) among
I have a VB project which I'm trying to configure with three build configurations.
Trying to use GridBagLayout. I have the method called buildLabel. This creates three label.

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.