2011-06-01 45 views
0

我的驗證工作得很好,但看似隨機,retured值不是我的模型集驗證錯誤,沒有什麼應該觸發錯誤,但一個「 1" 或‘2’則返回..CakePHP驗證()怪數組返回(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 
), 

回答

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-> data的值和Plan模型本身,以確保信息在那裏也是正確的。這可能是驗證器開始收集不好的數據。