Cakephp 2.x - 自定义验证不起作用

时间:2015-03-19 13:37:03

标签: php validation cakephp cakephp-2.0 cakephp-appmodel

我有以下代码,但根本无法使用。

在我看来,我有这个:

echo $this->Form->input('User.code', array(
            'between' => '<div class="input-group">',
            'after' => '</div>',
            'label' => array('text' => 'Geschenkcode<span>*</span><br><div class="form-small">Geben Sie hier den Geschenkcode<br>aus dem DVD-Flyer an!</div>', 'class' => 'control-label'),
            'div' => 'form-group',
            'class' => 'form-control'
        ));

在我的模型中有以下内容:

class User extends AppModel {
  public $validate = array(
      'code' => array(
        'isInCodes' => array(
            'rule' => array('isInCodes'),
            'message' => 'nenene'
          )
      ),
  );

  public function isInCodes($code) {
    return false;
  }
}

控制器:

public function contest($slug = null) {
    if (!$this->Contest->findBySlug($slug)) {
        throw new NotFoundException(__('Invalid Contest'));
    }
    if ($this->request->is(array('post', 'put'))) {
        if (!$this->User->validates()) {
            $errors = $this->User->validationErrors;
            debug($errors);
        }
        debug($this->request->data); exit();
        return $this->redirect(array('action' => 'index'));
    }
    $this->Contest->recursive = 0;
    $contest = $this->Contest->findBySlug($slug);
    $this->set('contest', $contest);
}

这根本不起作用。验证运行就像不进入函数一样。有什么建议吗?

问候

1 个答案:

答案 0 :(得分:2)

您需要在致电$this->User->set($this->request->data)

之前使用$this->User->validates()