当我尝试在我的模型中验证来自控制器的数据时,错误在数组中发生了两次invalidFields
,但我不明白为什么。
这是我的代码:
<?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
)
为什么我两次收到此错误?我已经尝试重命名字段,减少验证规则并使用不同的模型和控制器/控制台进行测试。但所有人都有相同的行为。
我找不到任何关于此的错误报告。如果有人可以帮助我,我会很高兴。