CakePHP:帮我验证错误(当数据有该字段时,为什么错误是miss字段)

时间:2010-10-19 01:22:20

标签: cakephp validation

图库模型

var $validate = array(
          'id' => array('rule' => 'numeric', 'required' => true, 'allowEmpty' => false, 'on' => 'update'),
          'name' => array('rule' => 'notEmpty', 'required' => true, 'allowEmpty' => false, 'on' => 'create'),
          'model' => array('rule' => 'notEmpty', 'required' => true, 'allowEmpty' => false, 'on' => 'create'),
          'model_id' => array('rule' => 'numeric', 'required' => true, 'allowEmpty' => false, 'on' => 'create'),
          'status' => array('rule' => array('inList', array('Draft', 'Publish')), 'message' => 'FAILED', 'allowEmpty' => false)
      );

动作控制器

  private function admin_edit() {
        if (!empty($this->data['Gallery']['id'])) {
              debug($this->data);
              if ($this->Gallery->save($this->data)) {

              }
              debug($this->Gallery->invalidFields());

        }
        $this->redirect('/');
  }

这是错误,请帮帮我

app\controllers\galleries_controller.php (line 149)
Array
(
    [Gallery] => Array
        (
            [id] => 38
            [name] => Chap 6
            [status] => Publish
        )

)

app\controllers\galleries_controller.php (line 153)
Array
(
    [id] => This field cannot be left blank
)

为什么当数据有id字段时,错误会错过id,MODEL不使用行为,只有ContainableBehavior

1 个答案:

答案 0 :(得分:1)

你不应该对你的id字段进行验证... cake会自动处理所有这些......

如果未设置id,则会创建新条目,如果已​​设置,则会更新。