我想对特定操作使用特定的验证,它们都具有相同的字段(用户名)。cakephp中的多重验证
结果:它不起作用,它的行为就像没有验证一样(我的'您的个人资料已更新',无论用户名长度如何)。
注:一些这些行动都是在同一视图(REG,日志)(不知道这是否改变任何东西)
我使用CakePHP版本2.5.4。
user.php的:
class User extends AppModel
{
public $name = 'User';
public $validate = array('reg' => array('username' => array('rule'=>array('minLength',6)))
,'edit' => array('username' => array('rule'=>array('minLength',4)))
,'log' => array('username' => array('rule'=>array('minLength',2)))
);
}
UsersController.php:
public function edit($id)
{
$this->User->validate=$this->User->validate['edit'];
$info = $this->User->findById($id);
if ($this->User->validates())
{
if ($this->request->is(array('info','put')))
{
$this->User->id = $id;
if ($this->User->save($this->request->data))
{
$this->Session->setFlash(__('Your profile has been updated.'));
}
$this->Session->setFlash(__('Unable to update your profile.'));
}
if (!$this->request->data)
{
$this->request->data = $info;
}
}
}
它的工作原理,vielen dank hmic和yes $ this-> User-> validates()在这里没有意义,但是我现在出现了一个问题,验证错误消息显示在视图中的每个用户名字段下,即使它是空的。 – exchanger3145 2014-10-30 11:00:53
来解释更多:我有注册和登录表单都有一个用户名字段,当我写的东西不允许注册用户名,我得到的消息两次,在两种形式 – exchanger3145 2014-10-30 11:06:09
我将添加登录功能的代码上面 – exchanger3145 2014-10-30 11:08:53