2012-12-10 215 views
0

我已经检查了cakephp验证了很长时间来验证我的登录表单。我的问题是当我输入用户名和密码作为空白验证没有显示。在UserController.phpcakephp登录表单验证的空白用户名和密码

登录功能包含

if ($this->request->is('post')) { 
      $this->user->set($this->request->data); 
      $errors = $this->user->invalidFields(); 

      if ($this->Auth->login()) { 
       return $this->redirect($this->Auth->redirect()); 
      } else { 
       $this->Session->setFlash($this->Auth->authError, 'default', array(), 'auth'); 
       $this->redirect($this->Auth->loginAction); 
      } 
     } else { 

      if ($this->Auth->login()) { 
       return $this->redirect($this->Auth->redirect()); 
      } 
     } 

我user.php的模型包含验证为

public $validate = array(
     'username' => array(
      'isUnique' => array(
       'rule' => 'isUnique', 
       'message' => 'The username has already been taken.', 
      ), 

      'notEmpty' => array(
       'rule' => 'notEmpty', 
       'message' => 'This field cannot be left blank.', 
      ), 
     ), 
     'email' => array(
      'email' => array(
       'rule' => 'email', 
       'message' => 'Please provide a valid email address.', 
      ), 
      'isUnique' => array(
       'rule' => 'isUnique', 
       'message' => 'Email address already in use.', 
      ), 
     ), 
     'password' => array(
      'rule' => array('minLength', 6), 
      'message' => 'Passwords must be at least 6 characters long.', 
     ), 
     'current_password' => array(
      'rule' => '_identical', 
      ), 
     'name' => array(
      'rule' => 'notEmpty', 
      'message' => 'This field cannot be left blank.', 
     ), 
    ); 

其实我的登录表单只包含usgername和密码。但我已经为用户注册表单设置了此验证。验证以注册形式正确工作,但在登录时验证不起作用。是的,我知道在这个网站上发布了很多关于同一问题的问题,但没有解决我的问题,我尝试了所有的stackoverflow问题。

$this->Model->validates(); 

你没有得到验证错误,因为你没有实际验证您的数据:请帮助

+0

验证用户详细信息没有意义,可以登录或不登录。 – dogmatic69

回答

0

验证只保存时,或当您直接调用验证方法使用发生。要获得验证错误,您需要执行以下操作:

if ($this->request->is('post')) { 
     $this->User->set($this->request->data); 
     if ($this->User->validates()) { 
      echo "This is valid!"; 
     } else { 
      echo "This is invalid"; 
      $errors = $this->User->validationErrors; 
     } 
} 
+1

调用“$ errors = $ this-> User-> invalidFields();”调用验证之后是错误的。您需要改用$ this-> User-> validationErrors! – mark

+0

良好的标记,你是正确的,因为这将重复验证错误。谢谢。 – shoesole