Welcome Guest, Not a member yet? Register   Sign In
codeigniter form validation callback function with multiple arguments issue
#12

[eluser]davidbehler[/eluser]
Why is there no point?
It's not hardcoded into the callback function but rather into the list of rules that are being applied to a certain field.

You can still use a different string for a different field:
Code:
$this->form_validation->set_rules('alias', 'Alias_Exist', 'trim|xss_clean|callback_alias_exist_check[livestock.alias]');  
$this->form_validation->set_rules('username', 'Username_Exist', 'trim|xss_clean|callback_alias_exist_check[user.name]');  
$this->form_validation->set_rules('foobar', 'Foobar_Exist', 'trim|xss_clean|callback_alias_exist_check[foo.bar]');

and if your callback function looks like this, then 3 different queries will be run:
Code:
function alias_exist_check($value, $str)
{
   $parts = explode('.', $str);
   $this->db->from($parts[0]);
   $this->db->where($parts[1], $value);
   $result = $this->db->get();

   echo $this->db->last_query();

   if($result->num_rows() > 0) {
      $this->form_validation->set_message('alias_exist_check', 'Already exists.');
      return FALSE;
   } else {
      return TRUE;
   }
}

Or even if you don't have multiple fields in one form that you need to run this check on, you could move the callback function into your own controller class (MY_Controller) and use it in multiple controllers for different fields.

Btw: I would move the db related stuff into the model, but that's up to you.


Messages In This Thread
codeigniter form validation callback function with multiple arguments issue - by El Forum - 03-13-2011, 09:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB