Cakephp 2.x - 自定义验证不工作
Cakephp 2.x - Custom Validation not working
我有以下代码,但它根本不起作用。
在我看来我有这个:
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);
}
这根本不起作用。验证贯穿始终,就像它没有进入函数一样。有什么建议吗?
问候
您需要先使用 $this->User->set($this->request->data)
才能调用 $this->User->validates()
我有以下代码,但它根本不起作用。
在我看来我有这个:
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);
}
这根本不起作用。验证贯穿始终,就像它没有进入函数一样。有什么建议吗?
问候
您需要先使用 $this->User->set($this->request->data)
才能调用 $this->User->validates()