0

I have checked cakephp validation for a long time to validate my login form. My problem is when i enter username and password as blank the validation not showing.

Login function in UserController.php contains

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

My User.php Model contains validator as

 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.',
        ),
    );

Actually my login form contains only usgername and password. But i have set this validation for userregistration form. The validation worked correctly in registration form but in the case of login the validation not working. Yes io know there is lot of questions posted on this website itself regarding the same issue, but nothing solve my problem, i have tried all the stackoverflow questions. Please Help

4

1 回答 1

0

验证仅在保存时发生,或者当您使用以下方法直接调用验证方法时:

$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;
        }
} 
于 2012-12-10T20:46:11.447 回答