简体   繁体   English

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

[英]Cakephp 2.x - Custom Validation not working

I have following code and it is not working at all. 我有以下代码,它根本不起作用。

In my view I have the this: 我认为这是:

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'
        ));

In my Model the following: 在我的模型中,以下内容:

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

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

Controller: 控制器:

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);
}

This is not working at all. 这根本不起作用。 Validation runs through like it is not going into the function. 验证过程就像没有进入功能一样。 Any suggestions? 有什么建议么?

regards 问候

您需要在调用$this->User->validates()之前使用$this->User->set($this->request->data) $this->User->validates()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM