2

Is there a way to validate data (using CakePHP's model validation) to make sure that at least "a" or "b" has data (does not have to have data in both).

Dave
  • 28,833
  • 23
  • 113
  • 183

3 Answers3

4

In your model, do something like this. The function will be called when you perform a save operation.

EDITED

public $validate = array(
    'a' => array(
        'customCheck' => array(
            'rule' => 'abCheck',
            'message' => 'You must enter data in a or b.'
        )
    ),
    'b' => array(
        'customCheck' => array(
            'rule' => 'abCheck',
            'message' => 'You must enter data in a or b.'
        )
    )
);

//Function must be public for Validator to work
//Checks to see if either a or b properties are set and not empty
public function abCheck(){
    if((isset($this->data['Model']['a']) && !empty($this->data['Model']['a'])) || (isset($this->data['Model']['b']) && !empty($this->data['Model']['b']))){
         return true;
    }
    return false;
}
Scott Harwell
  • 7,457
  • 2
  • 28
  • 41
  • this appears to be exactly what I was looking for, thanks! I'll try tomorrow and mark as answer if/when I get it to work. Thanks again. – Dave Jun 02 '11 at 01:50
  • @Scott Hmmm... not working. There was one parenthesis off - fixed that, but still can't get it tow work. Still trying though. – Dave Jun 02 '11 at 15:07
  • You've got the code in the model you are saving, correct? Are you getting any specific errors or is nothing happening? You should get something returned in $this->ModelName->invalidFields() to reference. You could also try to print_r something from the model to see if the function is even being called. – Scott Harwell Jun 02 '11 at 15:10
  • @Scott - The code is in the model I'm saving, yes. I'm not getting any errors, it's just not validating (ie it's submitting even w/out either data). I can print_r in the model?? Where will that show up? – Dave Jun 02 '11 at 15:11
  • @Scott - I'm getting my "message" for both fields (ie - not validating for either). They're select dropdowns - but didn't seem like that should matter – Dave Jun 02 '11 at 15:43
  • @Scott - WORKED! If you'd like to update your answer by removing one of the parenthesis after "!empty($this->data['a']" and adding the model to the array checks - ie $this->data['Model']['a'] then I'll mark it as the answer! Thanks again! – Dave Jun 02 '11 at 16:17
  • if you are pushing your code to a test server, you can just print_r($variable);exit; and it will display that variable without loading the rest of the page. If you are pushing to production before you can test, then don't do this. In your save function, your aren't limiting the validation, correct? – Scott Harwell Jun 02 '11 at 17:04
  • ah, started writing that last comment an hour ago and just saw your answer. Will update. – Scott Harwell Jun 02 '11 at 17:04
1

you can validate those conditions through "custom validations".

see this: adding your own validation

alexdd55
  • 1,109
  • 15
  • 24
  • I've seen/read that page, but it doesn't really give any examples that even lead me in the direction of what I'm hoping to do. Do you know of any pages that have better/more examples, or can you lead me in the right direction of what to do in the custom validation function(s)? – Dave Jun 01 '11 at 17:27
  • Can you post up the controller action code in which you are trying to validate? – OldWest Jun 01 '11 at 18:46
  • validation should be done in the model as long as possible to keep your code clean – alexdd55 Jun 01 '11 at 19:52
0

Try this instead:

public $validate = array(
     'a' => array(
         'customCheck' => array(
             'rule' => array('abCheck', 1),
             'message' => 'You must enter data in a or b.'
         )
     ),
     'b' => array(
         'customCheck' => array(
             'rule' => array('abCheck', 1),
             'message' => 'You must enter data in a or b.'
         )
     )
 );

 //Function must be public for Validator to work
 //Checks to see if either a or b properties are set and not empty
 public function abCheck(){
     if((isset($this->data['Model']['a']) && !empty($this->data['Model']['a'])) > || (isset($this->data['Model']['b']) && !empty($this->data['Model']['b']))){
          return 1;
     }
     return -1;
 }
Dibyendu Mitra Roy
  • 1,604
  • 22
  • 20