简体   繁体   中英

CakePHP validates() odd array return (1,2) value behaviour

My validation is working just fine, but seemingly randomly, the retured values are not my model set validation errors, and nothing should be triggering the error, but a "1" or "2" is returned..

Even if all field data is valid, I sometimes get a return value in my array of "1" or "2"...

Here is my validation in my controller:

 if ($this->RequestHandler->isAjax()) {
        if ($this->Plan->validates()) {
            $this->Plan->set($this->data);
            $errors = $this->Plan->invalidFields();
            $this->set('errors', $errors);
        } else {
            $this->Plan->set($this->data);
        }
 }

And here is my model validation:

        'ApplicantAge'   => array(
array('rule'=> array('maxLength', 3),
      'message' => 'Applicant Age cannot be longer than 3 digits.'),
      'last' => true,
array('rule'    => array('range', -1, 120),
      'message' => 'Applicant Age cannot exceed 120.'),
      'last' => true,
array('rule'    => 'numeric',
      'message' => 'Applicant Age must be a valid age.'),
      'last' => true,
),
    'SpouseAge'      => array(
array('allowEmpty' => true),
        'last' => true,
array('rule'    => array('maxLength', 3),
      'message' => 'Spouse Age cannot be longer than 3 digits.'),
      'last' => true,
array('rule'    => array('range', -1, 120),
      'message' => 'Spouse Age cannot exceed 120.'),
      'last' => true,
array('rule' => 'numeric',
      'message' => 'Spouse Age must be numeric.'),
      'last' => true,
),
    'NumberChildren' => array(
array('allowEmpty' => true),
      'last' => true,
array('rule'    => array('range', -1, 20),
      'message' => 'Number of Children cannot exceed 20.'),
      'last' => true,
array('rule'    => 'numeric',
      'message' => 'Number of Children must be numeric.'),
      'last' => true,
),
    'ZipCode' => array(
array('rule'    => array('postal', null, 'us'),
      'message' => 'Zip Code format must be valid. Example: 97756'),
      'last' => true,
array('rule'    => array('minLength', 5),
      'rule'    => array('maxLength', 5),
      'message' => 'Zip Code must be 5 digits.'),
      'last' => true
),

Shouldn't you be setting the data in the model before validating it?

if ($this->RequestHandler->isAjax()) {
    $this->Plan->set($this->data);
    if ($this->Plan->validates()) {
        //do something since data validates
    } else {
        $errors = $this->Plan->invalidFields();
        $this->set('errors', $errors);
    }
}

I know that you said it was random, but are you able to find any consistency in the error? You may be able to return the values for $this->data and the Plan model itself to make sure that the information is correct there as well. It may be that the validator is getting bad data to begin with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM