我需要一个 Cakephp 2.2 专家来帮助我。我想使用以下“$this->data”进行更新:
array(
'Button' => array(
(int) 0 => array(
'id' => '1',
'on' => '0'
),
(int) 1 => array(
'id' => '2',
'on' => '0'
),
(int) 2 => array(
'id' => '3',
'on' => '1'
),
(int) 3 => array(
'id' => '4',
'on' => '1'
)
)
)
Button 模型中的验证规则是:
public $validate = array(
'id'=>array(
'notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'Module id can not be empty',
'on' => 'update'
),
'naturalNumber' => array(
'rule' => 'naturalNumber' ,
'message' => 'Module id is not integer',
'on' => 'update'
)
),
'name'=>array(
'notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'Module name can not be empty',
'on' => 'create'
),
'alphaNumericWithSpaces' => array(
'rule' => array('custom', '/^[a-z0-9 ]*$/i') ,
'message' => 'Module category is not alphanumeric'
)
),
'type'=>array(
'notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'Module category can not be empty',
'on' => 'create'
),
'naturalNumber' => array(
'rule' => 'naturalNumber',
'message' => 'Module category is not integer'
)
),
'on'=>array(
'boolean' => array(
'rule' => 'boolean',
'message' => 'Module ON value is not boolean'
)
));
然后,在控制器中,我有以下代码:
if(!empty($this->data)) {
if($this->Button->saveAll($this->data)) {
debug ('Saved!');
}
else {
debug($this->Button->validationErrors);
}
}
问题是 'saveAll()' 没有进行更新,并引发以下验证错误,即使我在这些字段上使用了 => 'create' 以仅在创建记录时触发:
array(
'name' => array(
(int) 0 => 'Module name can not be empty'
),
'type' => array(
(int) 0 => 'Module category can not be empty'
)
)
谢谢!