[英]CakePHP $this->Plan->validates() Validation from non-model form
[英]Cakephp 2.+ $this->Model->validates(); always return true
我已经搜索了stackoverflow。 大多数问题都类似于这个问题,但是对我没有帮助。 我已经阅读cakephp 2.0的文档,但仍然找不到我的答案。
我的问题是:我在视图中有单独的表单。 每个帖子都有一个不同的$ this-> request-> data。 我想要哪个 我希望每个表单都使用此模型的验证子集。 但是问题在于它们总是“返回真实”。 即使我已经指定了fieldList。
视图:
<?php echo $this->Form->create('User'); ?>
<?php echo $this->Form->input('Postcode');?>
<?php echo $this->Form->input('Postcodeletter');?>
<?php echo $this->Form->submit('Submit');?>
<?php echo $this->Form->end();?>
<?php echo $this->Form->create('User'); ?>
<?php echo $this->Form->input('Password');?>
<?php echo $this->Form->input('checkPassword');?>
<?php echo $this->Form->submit('Submit');?>
<?php echo $this->Form->end();?>
控制器:
unset($this->request->data['User']['Id']);
$this->User->set($this->request->data);
if ($this->User->validates(array('fieldList' => array('Postcode')))) {
//valid
} else {
//error
}
模型:
class User extends AppModel {
public function beforeSave($options = array()){
if(isset($this->data['User']['Password'])){
$this->data['User']['Password'] = AuthComponent::password($this->data['User']['Password']);
}
return true;
}
public $primaryKey = 'Id';
public $validate = array(
'Firstname' => array(
'rule' => 'notEmpty',
'allowEmpty' => false,
'on' => 'create',
'message' => '*Vult uw voornaam in'
),
'Postcode' => array(
'numeric' => array(
'rule' => 'numeric',
'allowEmpty' => false,
'on' => 'create',
'message' => 'Needs to be numbers'
),
'minLength' => array(
'rule' => array('minLength',4),
'allowEmpty' => false,
'on' => 'create',
'message' => 'Min 4 numbers'
)
)
);// end $validate
}
我在“如果陈述”中做错了什么吗?
附言:我已经缩短了代码,我认为问题很明显,对于我的英语不好,我感到抱歉。
更新:
添加了$this->User->set($this->request->data);
现在添加了unset($this->request->data['User']['Id'])
。
在Core-Config中,您的Debug-Level是否设置为2? (app / Config / core.php)
您可以在Controller中将此问题的输出添加到您的问题吗?
// Put this before your If-Statement
debug( $this->User->invalidFields(); );
// You may copy the rendered output
我不了解您的软件,但是经常能帮助我弄清楚为什么蛋糕行为不符合我的期望的原因是,例如,使用PHP-IDE和X-Debug调试我的代码。
如果没有理由,请不要使用'on' => 'create'
。 在您的情况下不是。
您可能希望始终验证minLength或notEmpty,即使在编辑时也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.