Welcome Guest, Not a member yet? Register   Sign In
Use Models, Libraries and Helpers in Form Validation rules
#4

[eluser]Ashes-to-Ashes[/eluser]
I am running something like this:

Code:
// in library:
function general_form ($form_name)
{
       if ($form_name == 'big_form')
       {
              $this->big_form();
       }
}

function big_form()
{
      $this->set_rules ();
      if ($this->CI->form_validation->run () == false)
      {
            // display form
      }
      else
      {
         // process form
      }
}

function set_rules()
{
      $this->CI->form_validation->set_rules ('account_number', 'Account Number', 'external_callback[model.accounts_model.exists]');
       // more rules here once I get it working
}


// in MY_Form_validation.php
class MY_Form_validation extends CI_Form_validation
{
public function external_callback($postdata, $extra)
{  
  echo 'Attempting Model Call Back';
  //Separate out the parts we need from $extra
  list($external_type, $name, $function) = explode('.', $extra);
  $external_type = strtolower($external_type);
  
  $CI =& get_instance();
  
  //Try loading the resource!
  $CI->load->$external_type($name);
  
  // Strip the parameter (if exists) from the rule
  // Rules can contain a parameter: max_length[5]
  $param = FALSE;
  if (preg_match("/(.*?)\[(.*)\]/", $function, $match))
  {
   $function = $match[1];
   $param = $match[2];
  }
  
  //If library or model, get the method
  $result = FALSE;
  
  if(($external_type == 'library' || $external_type == 'model') && method_exists($CI->$name, $function))
  {
   $result = ( ! empty($param)) ? $CI->$name->$function($postdata, $param) : $CI->$name->$function($postdata);
  }
  //it was a helper, just use function instead...
  else if(function_exists($function))
  {
   $result = ( ! empty($param)) ? $function($postdata, $param) : $function($postdata);
  }
  
  //return the final result of the external callback back to validation
  return $result;
}
}// end class

// in model

public function exists($str)
{
  $CI =& get_instance();
  $sql = "select * from accounts where number = '$str'";
  $query = $this->db->query ($sql);
  if ($query->num_rows == '1')
  {
   return false;
  }
  else {
   //note we must use 'external_callback' as the error message instead of 'test_model' like you normally would
   $CI->form_validation->set_message('external_callback', "%s: $str does not exist");
    return ($str);
  }
}

It appears to never call the call back function


Messages In This Thread
Use Models, Libraries and Helpers in Form Validation rules - by El Forum - 11-28-2012, 05:02 PM



Theme © iAndrew 2016 - Forum software by © MyBB