title: Can't set error message in Code Igniter 3 when using callback function
body: <p>I am writing a method that uses <code>POST</code> variables posted by <code>AJAX</code> to add a user to a certain course in the database, but I can't get the callback to work correctly:</p>
  
  <pre><code> public function enroll()
  {
      $package = array();
      $this-&gt;load-&gt;library('form_validation');
      $this-&gt;form_validation-&gt;set_rules('course', 'Vak', 'required|callback_not_enrolled');
      $fields = array("course");
  
      if ($this-&gt;form_validation-&gt;run($this) === FALSE) {
          $errors = array();
          $success = array();
          foreach ($fields as $field) {
              $error = form_error($field);
              if ($error !== "") {
                  $errors[$field] = $error;
              } else {
                  $success[$field] = True;
              }
          }
          $package["field_errors"] = $errors;
          $package["field_success"] = $success;
          $package["success"] = False;
      } else {
          $package["database"] = $this-&gt;course_model-&gt;enroll_user($this-&gt;data["user"], $this-&gt;input-&gt;post("course"));
          $package["success"] = True;
      }
      echo json_encode($package);
  }
  </code></pre>
  
  <p>I wrote the callback <code>not_enrolled</code> to check if the user is not already enrolled to the database. Note that I can't use <code>is_unique</code> because I have to test the combined uniqueness of two fields (so just one or two separate ones don't do the trick) and the <code>id</code> of the user is not included in the form (because it's part of the Code Igniter session). </p>
  
  <p>The callback function:</p>
  
  <pre><code>    public function _not_enrolled($course)
      {
      $exists = ($this-&gt;user-&gt;is_enrolled($course, $this-&gt;data["user_id"]) != False);
      if ($exists != False) {
          $this-&gt;form_validation-&gt;set_message("not_enrolled", "Already enrolled");
          return False;
      } else {
          return True;
      }
  }
  </code></pre>
  
  <p>And finally the method <code>is_enrolled</code> from the model:</p>
  
  <pre><code>    public function is_enrolled($course, $user=False) {
      if($user==False){
          $user = $this-&gt;data["user_id"];
      }
      $this-&gt;db-&gt;select()-&gt;from("course_participant")-&gt;where("user_id", $user)-&gt;where("course_id", $course);
      $query = $this-&gt;db-&gt;get();
      return($query-&gt;num_rows()&gt;0);
  }
  </code></pre>
  
  <p>Through a call to <code>var_dump($this-&gt;_not_enrolled($existing_course_id));</code> I know that both the callback function and the method from the model work, as it correctly returned <code>true</code>. 
  When I <code>var_dump</code> the <code>$package</code> array or <code>validation_errors()</code> I don't get any validation errors except that it says <em>Unable to access an error message corresponding to your field name Vak(not_enrolled).</em></p>
  
  <ol>
  <li>I tried removing the initial <em>_</em> from the function name but that gives me a Server Status 500 error. </li>
  <li>I have another setup <em>exactly</em> like this, albeit other database calls, with a callback using the same syntax. This method works perfectly.</li>
  </ol>