Validation Cakephp 2.&x2B$这->;型号->;验证();总是返回真值
我已经在stackoverflow中搜索过了。大多数问题都类似于这个问题,但对我没有帮助。我已经阅读了CakePHP2.0的文档,仍然找不到我的答案 我的问题是:我在视图中有不同的表单。每个帖子都有不同的$this->request->data。这正是我想要的。我希望每个表单都使用该模型验证的子集。但问题是它们总是“返回真”。即使我已经指定了字段列表 视图: 型号:Validation Cakephp 2.&x2B$这->;型号->;验证();总是返回真值,validation,cakephp,controller,Validation,Cakephp,Controller,我已经在stackoverflow中搜索过了。大多数问题都类似于这个问题,但对我没有帮助。我已经阅读了CakePHP2.0的文档,仍然找不到我的答案 我的问题是:我在视图中有不同的表单。每个帖子都有不同的$this->request->data。这正是我想要的。我希望每个表单都使用该模型验证的子集。但问题是它们总是“返回真”。即使我已经指定了字段列表 视图: 型号: class User extends AppModel { public function beforeSave($op
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
}
“如果声明”中是否有我做错的事情
Ps:我已经缩短了代码,我认为问题很明显,我为我糟糕的英语感到抱歉
更新:
添加了$this->User->set($this->request->data)代码>
添加了unset($this->request->data['User']['Id'])
现在它可以工作了。您的调试级别在核心配置中设置为2吗?(app/Config/core.php)
您能否将控制器中的输出添加到您的问题中
// Put this before your If-Statement
debug( $this->User->invalidFields(); );
// You may copy the rendered output
我不知道你的软件,但我经常发现cake的行为违背我的期望的原因是使用PHP-IDE和X-Debug调试我的代码。在没有正当理由的情况下,不要使用'on'=>'create'
。
你的情况不是这样
您可能希望始终验证minLength或notEmpty,即使在编辑时也是如此。调试级别为“2”。和调试($this->User->invalidFields());给了我“array()”,没有结果。你提交的Formdata肯定是无效的?我认为它是无效的。我要邮政编码至少4个号码。我刚刚输入了两个数字。我的模型的规则可能是错误的吗?在“//end postcode/”之后至少缺少一个(结束)括号“)”-如果您在代码中正确缩进数组,那么在每个括号之后就不需要注释-这样就很容易阅读;-)数据库字段的名称是什么?其余的代码在哪里?validates()永远不会单独工作。它总是需要先将数据传递给模型。@cornelb | Firstname | Postcode |@mark我忘了$this->User->set($this->request->data),我最近设置了这个,现在仍在调试。但我认为这是个问题。您应该向我们展示$this->request->data中的post数据,以及$this->User->validationErrors在validates()调用后包含的内容。
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
// Put this before your If-Statement
debug( $this->User->invalidFields(); );
// You may copy the rendered output