Cakephp登录表单验证空白用户名和密码



我已经检查了很长时间的cakephp验证来验证我的登录表单。我的问题是当我输入用户名和密码为空时,验证未显示。

用户控制器中的登录功能.php包含

 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());
            }
        }

我的用户.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.',
        ),
    );

实际上,我的登录表单仅包含用户名和密码。但是我已经为用户注册表单设置了此验证。验证在注册表中正常工作,但在登录的情况下,验证不起作用。是的,我知道这个网站上有很多关于同一问题的问题,但没有什么能解决我的问题,我已经尝试了所有堆栈溢出问题。请帮忙

仅在保存时或使用以下方法直接调用验证方法时进行验证:

$this->Model->validates();

不会收到验证错误,因为您实际上没有验证数据。若要获取显示验证错误,需要执行以下操作:

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;
        }
} 

最新更新