CakePHP验证()奇数数组返回(1,2)值行为

时间:2011-06-01 17:21:10

标签: arrays validation cakephp controller

我的验证工作得很好,但看似随机,返回的值不是我的模型集验证错误,没有任何东西应该触发错误,但返回“1”或“2”..

即使所有字段数据都有效,我有时会在“1”或“2”数组中获得返回值...

这是我在我的控制器中的验证:

 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);
        }
 }

这是我的模型验证:

        '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
),

1 个答案:

答案 0 :(得分:0)

在验证之前,您不应该在模型中设置数据吗?

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);
    }
}

我知道你说它是随机的,但是你能在错误中找到任何一致性吗?您可以返回$ this->数据和计划模型本身的值,以确保信息在那里也是正确的。可能是验证者开始收到错误的数据。