title: How to compare form input data to database data in a custom codeigniter form validator?
body: <p>Edit: Some naming had been mixed up in my attempts to solve it myself. I've fixed the callback etc naming and the same error persists.</p>
  
  <p>I am attempting to create a login page for my codeigniter website. I already have a registration page that correctly inputs usernames and passwords in to a "users" table. I am having issues understanding the syntax of creating the functions needed for custom form validators.</p>
  
  <p>My error is "Unable to access an error message corresponding to your field name" for the password and username custom validators.</p>
  
  <p>Here is the relevant part of the controller "login_ctrl"</p>
  
  <pre><code>    class login_ctrl extends CI_Controller {
  
  function __construct() {
  parent::__construct();
  $this-&gt;load-&gt;model('login_mdl');
  }
  function index() {
  
  $this-&gt;load-&gt;library('form_validation');
  
  $this-&gt;form_validation-&gt;set_error_delimiters('&lt;div class="error"&gt;', '&lt;/div&gt;');
  
  //Validating Name Field
  $this-&gt;form_validation-&gt;set_rules('username', 'Username', 'trim|required|callback_userCorrect');
  
  //Validating Password Field 
  $this-&gt;form_validation-&gt;set_rules('password', 'Password', 'trim|required|callback_passwordCorrect');
  
  //variables to pass form input
  $username = $this-&gt;input-&gt;post('username');
  $password = $this-&gt;input-&gt;post('password'); 
  
  //reload login page if validation fails
  if ($this-&gt;form_validation-&gt;run() == FALSE) {
  $this-&gt;load-&gt;view('login');
  } else {
  //functions for custom validation
  function userCorrect($username) {
      $this-&gt;load-&gt;library('form_validation');
      //the loads the model that contains the function to compare input to database data
      $userExists = $this-&gt;login_mdl-&gt;userExists($username);
  
      if ($userExists) {
          $this-&gt;form_validation-&gt;set_message(
             'userCorrect', 'correct user.'
      );
          return true;
      } else {
          $this-&gt;form_validation-&gt;set_message(
              'userCorrect', 'not a valid user name.'
          ); 
          return false;
      }
  }
  function passwordCorrect($password) {
      $this-&gt;load-&gt;library('form_validation');
      $passwordExists = $this-&gt;login_mdl-&gt;passwordCorrect($password);
  
      if ($passwordExists) {
          $this-&gt;form_validation-&gt;set_message('passwordCorrect', 'correct password.');
          return true;
      } else {
          $this-&gt;form_validation-&gt;set_message('passwordCorrect', 'invalid password.'); 
          return false;
      }
  }
  </code></pre>
  
  <p>This is the corresponding view "login"</p>
  
  <pre><code>&lt;?php echo form_open('login_ctrl'); ?&gt;
  &lt;h1&gt;Login&lt;/h1&gt;&lt;hr/&gt;
  &lt;?php if (isset($message)) { ?&gt;
  &lt;?php } ?&gt;
  &lt;?php echo form_label('User Name :'); ?&gt; &lt;?php echo form_error('username'); ?&gt;&lt;br /&gt;
  &lt;?php echo form_input(array('id' =&gt; 'username', 'name' =&gt; 'username')); ?&gt;&lt;br /&gt;
  
  &lt;?php echo form_label('Password :'); ?&gt; &lt;?php echo form_error('password'); ?&gt;&lt;br /&gt;
  &lt;?php echo form_input(array('id' =&gt; 'password', 'name' =&gt; 'password')); ?&gt;&lt;br /&gt;
  
  &lt;?php echo form_submit(array('id' =&gt; 'submit', 'value' =&gt; 'Submit')); ?&gt;
  
  &lt;?php echo form_close(); ?&gt;&lt;br/&gt;
  </code></pre>
  
  <p>Finally, this is the corresponding model "login_mdl" (I think the issue might be in this guy).</p>
  
  <pre><code>&lt;?php
  class login_mdl extends CI_Model{
  function __construct() {
  parent::__construct();
  }
  function userExists($username) {
      $this-&gt;db-&gt;select('id');
      $this-&gt;db-&gt;where('username', $username);
      $query = $this-&gt;db-&gt;get('users');
  
      if ($query-&gt;num_rows() &gt; 0) {
          return true;
      } else {
          return false;
      }
  }
  
  function passwordCorrect($password) {
      $this-&gt;db-&gt;select('password');
      $this-&gt;db-&gt;where('username', $username);
      $query = $this-&gt;db-&gt;get('users');
      $result = $query-&gt;row();
      return $result-&gt;password;
  
      if ('password' == $password) {
          return true;
      } else {
          return false;
      }
  }
  }
  ?&gt;
  </code></pre>
  
  <p>I think my issue is related to the db calls and if statements but I've been reading documentation and failing at fixing this for hours so a new pair of eyes would be greatly appreciated.</p>