1
當我嘗試從我的模型中的控制器驗證數據,然後在invalidFields
數組中發生錯誤兩次,但我不明白爲什麼。蛋糕PHP 2.x模型驗證被調用兩次
這裏是我的代碼:
<?php
App::uses('AppController', 'Controller');
App::import('Vendor','Excel');
class ServicedesksController extends AppController{
public $uses = array("CustomerInformation");
public function uploadData(){
$this->layout = 'index';
if($this->request->is('post')){
$safeData = array('name' => 'testkun', 'kkz' => 'bae');
$this->CustomerInformation->set($safeData);
if($this->CustomerInformation->validates()){
print_r("successful");
}else{
print_r($this->CustomerInformation->invalidFields());
print_r("not successful");
}
}
}
這是我的型號代碼:
<?php
class CustomerInformation extends AppModel{
public $validate = array(
'name' => array(
'rule' => array('minLength', '8'),
'message' => 'Minimum length of 8 is required'
),
'kkz' => array(
'rule-1' => array(
'rule' => '/^[A-Z]{3}$/i',
'message' => 'Only letters allowed'
),
'rule-2' => array(
'rule' => 'checkDuplicate',
'message' => 'There is still an existing entry for this kkz',
'last' => false
)
)
);
public function checkDuplicate($check){
$existingCount = $this->find('count', array(
'conditions' => $check
));
return $existingCount == 0;
}
}
當我執行uploadData
,invalidFields
包含
Array (
[name] => Array (
[0] => Minimum length of 8 is required
[1] => Minimum length of 8 is required
)
爲什麼我得到這個錯誤的兩倍?我嘗試過重命名這些字段,減少驗證規則並使用不同的模型和控制器/控制檯進行測試。但都有相同的行爲。
我找不到關於此的任何錯誤報告。如果有人能幫助我,我會很高興。
謝謝。這對我行得通。 – Michael