Cake PHP 2.x 模型验证被调用了两次
Cake PHP 2.x Model validation is called twice
当我尝试在我的模型中验证来自控制器的数据时,错误在 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
)
为什么我两次收到这个错误?我已经尝试重命名字段,减少验证规则并使用不同的模型和 Controllers/Consoles 进行测试。但是所有人都有相同的行为。
我找不到任何关于此的错误报告。如果有人能帮助我,我会很高兴。
来自食谱:
The validates
method invokes the invalidFields
method which populates the validationErrors
property of the model. The invalidFields method
also returns that data as the result:
$errors = $this->ModelName->invalidFields(); // contains validationErrors array
The validation errors list is not cleared between successive calls to invalidFields()
. So if you are validating in a loop and want each set of errors separately don’t use invalidFields()
. Instead use validates()
and access the validationErrors
model property.
因此,您应该调用:
print_r($this->CustomerInformation->validationErrors);
而不是
print_r($this->CustomerInformation->invalidFields());
当我尝试在我的模型中验证来自控制器的数据时,错误在 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
)
为什么我两次收到这个错误?我已经尝试重命名字段,减少验证规则并使用不同的模型和 Controllers/Consoles 进行测试。但是所有人都有相同的行为。
我找不到任何关于此的错误报告。如果有人能帮助我,我会很高兴。
来自食谱:
The
validates
method invokes theinvalidFields
method which populates thevalidationErrors
property of the model. TheinvalidFields method
also returns that data as the result:
$errors = $this->ModelName->invalidFields(); // contains validationErrors array
The validation errors list is not cleared between successive calls to
invalidFields()
. So if you are validating in a loop and want each set of errors separately don’t useinvalidFields()
. Instead usevalidates()
and access thevalidationErrors
model property.
因此,您应该调用:
print_r($this->CustomerInformation->validationErrors);
而不是
print_r($this->CustomerInformation->invalidFields());