[英]CakePHP validates() odd array return (1,2) value behaviour
我的驗證工作得很好,但似乎是隨機的,返回的值不是我的 model 設置驗證錯誤,應該不會觸發錯誤,但會返回“1”或“2”。
即使所有字段數據都有效,我有時也會在我的數組“1”或“2”中得到一個返回值......
這是我在 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);
}
}
這是我的 model 驗證:
'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
),
您不應該在驗證之前在 model 中設置數據嗎?
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 和計划 model 本身的值,以確保那里的信息也是正確的。 可能是驗證器一開始就收到了錯誤的數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.