Cake PHP custom validation rule

2019-08-28 04:00发布

问题:

I got a problem with a custom validation rule in Cake 2.X

I want to check if the entered zipcode is valid and therefore a function in the class zipcode is called from the class post.

But the validation returns false all the time.

Appmodel in class post (rule-3 is it):

'DELIVERYAREA' => array(
        'rule-1' => array(
            'rule' => array('between', 5, 5),
            'message' => 'Bitte eine fünfstellige Postleitzahl eingeben'
        ),
        'rule-2' => array(
            'rule' => 'Numeric',
            'message' => 'Bitte nur Zahlen eingeben'
        ),
        'rule-3' => array(
            'exists' => array(
                'rule' => 'ZipExists',
                'message' => 'Postleitzahl existiert nicht!'
            )
        )
    ),

Appmodel in class zipcode:

class Zipcode extends AppModel {
  var $name = 'Zipcode';

  var $validate = array(
    'zipcode' => array(
       'length' => array(
              'rule' => array('maxLength', 5),
              'message' => 'Bitte einen Text eingeben'
          ),
         'exists' => array(
          'rule' => array('ZipExists'),
          'message' => 'Postleitzahl existiert nicht!'

       )
    )         
  );

  function ZipExists($zipcode){

    $valid = $this->find('count', array('conditions'=> array('Zipcode.zipcode' =>$zipcode)));
    if ($valid >= 1){
      return true;
    }
    else{
      return false;
    }
  }

I hope it´s something stupidly easy? Thanks in advance

回答1:

I think this:

'Zipcode.zipcode' =>$zipcode

...needs to be this:

'Zipcode.zipcode' =>$zipcode['zipcode']



回答2:

Careful what you expect inside the validation rule. Use debug() etc to find out what exactly is coming in. $data is always an array here.

public function zipExists($data) {
    $zipcode = array_shift($data); // use the value of the key/value pair
    $code = $this->find('first', array('conditions'=> array('Zipcode.zipcode' =>$zipcode)));
    return !empty($code);
}


回答3:

try this for only model validation.

  function ZipExists(){

    $valid = $this->find('count', array('conditions'=> array('Zipcode.zipcode' =>$this->data['Zipcode']['zipcode'])));
    if ($valid >= 1){
      return true;
    }
    else{
      return false;
    }


回答4:

I found the solution. Cake wants the custom validation rules to be in the certain class where the rule is called. So, when you call a custom rule in class post, the custom function has to be written down in class post, otherwise cake won´t find it and validate it to false everytime.

The magic to do here is to import the appmodel-class you want to use in the class you call the validation-function. That works with the following statement:

$Zipcode = ClassRegistry::init('Class to use - in my case "Zipcode"');

But if your tables are associated with each other with hasAny or belongsTo and stuff, the custom function works without that. Another important point you mustn´t miss is, that all validation functions has to be introduced with "public function xyz" otherwise cake won´t find them too.