title: CakePHP Login Username/Password Validation
body: <p>I have the below in my users_controller because there is further logic needed to perform the login, however when done in CakePHP the default validation stops and therefore i am trying to force it to validate the username and password fields before doing the account check. Everything works fine but this. I have tried adding two if statements (below) to check if username/password is empty, however as soon as the page loads they are empty and the validation box shows.</p>
  
  <p>I am stumped as to how to achieve this. Any help would be greatly appreciated.</p>
  
  <pre><code>    function login() {
      if($this-&gt;Auth-&gt;user()) {
          $this-&gt;redirect(array('controller'=&gt;'shows'));
      }
      if(empty($this-&gt;data['User']['username'])) {
          $this-&gt;User-&gt;validationErrors['username'] = "Please enter a username";
      }
      if(empty($this-&gt;data['User']['password'])) {
          $this-&gt;User-&gt;validationErrors['password'] = "Please enter a password";
      }
      if(!empty($this-&gt;data['User']['username'])) {
          // unset unrequired validation rules
          unset($this-&gt;User-&gt;validate['username']['unique']);
  
          // validate form
          $this-&gt;User-&gt;set($this-&gt;data);
          if($this-&gt;User-&gt;validates()) {
              // update Last Login date
              $this-&gt;User-&gt;id = $this-&gt;User-&gt;_user['User']['id'];
              $this-&gt;User-&gt;saveField('last_login',date("Y-m-d H:i:s"));
  
              // save User to Session and redirect
              $this-&gt;Session-&gt;write('User', $this-&gt;User-&gt;_user);
              $this-&gt;Session-&gt;setFlash('You have successfully logged in.','default',array('class'=&gt;'flash_green'));
              //$this-&gt;redirect(array('controller'=&gt;'shows', 'admin'=&gt;FALSE));
          } else {
              $this-&gt;Session-&gt;setFlash('Incorrect username/password combination.','default',array('class'=&gt;'flash_red'));
              $this-&gt;redirect(array('controller'=&gt;'users', 'action'=&gt;'login', 'admin'=&gt;FALSE));
          }
      }
  }
  </code></pre>
  
  <p>users_controller beforeFilter()</p>
  
  <pre><code>    function beforeFilter(){
      parent::beforeFilter();
      $this-&gt;Auth-&gt;allow('register');
  }
  </code></pre>
  
  <p>app_controller beforeFilter and components</p>
  
  <pre><code>    var $components = array('Session', 'Auth' =&gt; array(
      'loginAction' =&gt; array('controller'=&gt;'users','action'=&gt;'login', 'admin'=&gt;false),
      //'logoutRedirect' =&gt; array('controller'=&gt;'users','action'=&gt;'logout'),
      'loginRedirect' =&gt; array('controller'=&gt;'shows', 'action'=&gt;'index'),
      'autoRedirect' =&gt; false,
      'authorize' =&gt; 'controller')
  );
  
  function beforeFilter() {
      $this-&gt;Auth-&gt;allow('home');
      $this-&gt;set('admin', $this-&gt;_isAdmin());
      $this-&gt;set('logged_in', $this-&gt;_loggedIn());
      $this-&gt;set('users_username', $this-&gt;_usersUsername());
  }
  </code></pre>