CodeIgniter Form Validation Callback Functions with Multiple Arguments

If you’re reading this you most likely already know that you can only send one argument to callback functions using CodeIgniter’s Form Validation Class. If you are not yet familiar with this feature/limitation, here is an example of how the callback works.

Here is our controller with a normal callback (_foo) and our multiple-argument callback function (_valid_login)…

class Login extends Controller {
 function Login()
 function _foo($str = '', $val = '') // normal callback function
  if ($str != $val) {
   $this->form_validation->set_message('_foo', 'The %s field must be ' . $val);
   return FALSE;
  return TRUE;
 function _valid_login($str = '', $params = '') // callback function with multiple parameters
  list($username, $password) = explode(',', $params);
  if ($this->users->valid_login($username, $password)) {
   return TRUE;
  $this->form_validation->set_message('_validate_login', 'No matching accounts!');
  return FALSE;
 function index()
  $username = $this->input->post('username', TRUE);
  $password = $this->input->post('password', TRUE);
  $params = "{$username},{$password}";
  $this->form_validation->set_rules('username', 'Username', "trim|required|valid_email|strtolower|xss_clean|callback__foo[bar]|callback__valid_login[{$params}]");
  $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|xss_clean');
  if ($this->form_validation->run() == FALSE) {
   // invalid or initial request
  } else {
   // success

The model…

class Users extends Model {
 function Users(){
 function valid_login($username = '', $password = '')
  $this->db->where('username', $username);
  $this->db->where('password', $password);
  return $this->db->count_all_results($this->table);

And the view…

<form action="login" method="post">
   <td><input name="username" type="text" /><?php echo form_error('username'); ?></td>
   <td><input name="password" type="password" /><?php echo form_error('password'); ?></td>
   <td><input type="submit" value="Log In" /></td>

Our _foo() callback function is what you might normally expect, where only a single argument is passed. If you need to send multiple parameters however, our _validate_login example function is about as simple as it gets.

A quick note about the _validate_login function… The $str parameter represents what is always passed to any form validation function, the field’s current value. We are ignoring the first argument for the sake of this example so don’t bother pointing out that we could have used that to get the username 😉

The only thing you really need to do to pass multiple arguments is expode() the string passed to our function into an array and assign the parts (2 for this example) to variable names using PHP’s list() function.

There’s not much else to it. Now you know how to pass multiple arguments to a CodeIgniter form validation callback function 🙂

9 thoughts on “CodeIgniter Form Validation Callback Functions with Multiple Arguments

  1. Dave Kiss

    Great post. In my case, I needed to pass an array that maintained the key/value pairs, so I chose to use php’s http_build_query function to convert the array to a string, and then once inside the callback, use php’s parse_str function to decode the query back into an array.

  2. Robert Mullaney Post author

    Glad my post helped.

    Not sure if there are any performance differences but you could also try serialize() and unserialize() to pass an array or object to your function (very useful for multi-dimensional arrays or complex objects) 🙂

  3. Robert Mullaney Post author

    Reagrding my previous reply…

    You would need to use urlencode(serialize($myarray)) and urldecode(unserialize($myarray)) to ensure the data passes correctly in the URL. In which case, your method of using http_build_query() and parse_str() may be likely to outperform my method.

    My method still applies however for nested arrays and objects. Which may make it better in general since you only need one method to parse the arguments regargless of how complex they may become 😉

  4. ashoksharmaz87

    When I am trying to validate my form. I am getting this error- “Unable to load the requested class: validation”
    Can you help me to find whats the problem.?

    my code is :


    public function insert_student()
    $this->load->helper(array(‘form’, ‘url’));
    $this->form_validation->set_rules(‘sname’, ‘username’, ‘required’);
    $this->form_validation->set_rules(’email’, ‘Email’, ‘required’);
    $this->form_validation->set_rules(‘password’, ‘password’, ‘required’);
    $rules[‘vpassword’] = “required”;
    $rules[‘address’] = “required”;
    $rules[‘gender’] = “required”;
    $rules[‘hobbies’] = “required”;
    $rules[‘class’] = “required”;

    if ($this->validation->run() == FALSE)


    1. Robert Mullaney Post author

      Take another look at your code… $this->load->library(‘validation’); should be $this->load->library(‘form_validation’);

      Also, replace $this->validation with $this->form_validation in the rest of your code.

      In other words, you’re mixing old and new versions of the validation libraries 😉

  5. Vytautas Galdikas

    Well if you really need some outside data in that function, why not use a global variable? Of course not good practice to start making global variables. But definitely one solution to a problem! 🙂

    1. admin Post author

      My method uses the same principles as the core functions. Not sure why you’d want to reinvent the wheel and do things differently with globals when it’s relatively easy to create callbacks that accept parameters 😉

  6. harrid

    i am getting this error “Unable to load the requested class: validation”, can you help me to find whats the problem ?
    my code :
    function index(){

    $fields[‘namadepan’]=’Nama Depan’;
    $fields[‘namabelakang’]=’Nama Belakang’;
    $fields[‘alamatemail’]=’Alamat Email’;

    if ($this->form_validation->run() ==FALSE){
    else {

    $data[‘title’]=’sedang mengirim email…’;
    $data[‘header’]=’sedang mengirim email…’;
    $data[‘message’]=$this->email->send()?’pesan email berhasil dikirim!’:’Gagal mengirim pesan email!’;

    1. admin Post author

      First things first… $this->load->helper(‘form’) is not necessary. $this->load->library(‘form_validation’) automatically includes that helper. I don’t see anywhere in your code where you called (the deprecated library) $this->load->library(‘validation’) which is what would throw that error. More complete code will be necessary to help you 😉


Leave a Reply

Your email address will not be published. Required fields are marked *