Welcome Guest, Not a member yet? Register   Sign In
Validation callback with input value as argument
#1

[eluser]Santiag0[/eluser]
Hello there!
I have this form to edit the users

Code:
<input type="text" name="username[]" value="Foo" >
<input type="hidden" name="user_id[]" value="20" >
<input type="password" name="password[]" value="Foo" >

<input type="text" name="username[]" value="Bar" >
<input type="hidden" name="user_id[]" value="21" >
<input type="password" name="password[]" value="Bar" >

I want to check if the new name of the user is already taken by other user.
I think that i can do this using a callback, like

Code:
$this->form_validation->set_rules('username[]', 'user name', 'trim|required|callback__user_exists[user_id[]]');

function _user_exists($username, $user_id)
  {
      if ($this->admin_users_model->user_exists($username, $user_id)
      {
          $this->form_validation->set_message('_user_exists', 'The name '. $username .' is already taken.');
          return FALSE;
      }
      else
          return TRUE;
  }

But looks that i can't pass the value of other input as argument of a callback.
Please, how can I make a callback with 2 input values?
I tried with $this->input('username') but returns an array, and I need to run the callback with the corresponding user_id.

I need this to do a query like
Code:
$this->db->get_where('users', array("username"=>$username, "user_id !="=>$user_id));

Thanks in advance for your help!
Sorry for my poor english.
#2

[eluser]pistolPete[/eluser]
Do you really need to edit more than one database entry per page?

By the way:

Code:
// you need TWO underscores here: callback_ + _user_exists
$this->form_validation->set_rules('username[]', 'user name', 'trim|required|callback__user_exists');
#3

[eluser]bgreene[/eluser]
i'm only a beginner but this is how i did it. the logic? being that username is valid if he hasn't changed it or if it is still available.
function username_check($newname)
{
$crntname = $this->dx_auth->get_username();
$result = ( (strcasecmp($crntname,$newname) == 0) OR $this->dx_auth->is_username_available($newname));
if ( ! $result)
{
$this->form_validation->set_message('username_check', 'This Username is already in use. Please choose another username.');
}
return $result;
}
#4

[eluser]Santiag0[/eluser]
[quote author="pistolPete" date="1239713906"]Do you really need to edit more than one database entry per page?

By the way:

Code:
// you need TWO underscores here: callback_ + _user_exists
$this->form_validation->set_rules('username[]', 'user name', 'trim|required|callback__user_exists');
[/quote]

Sorry, I make a mistake in that line, but is not the point.
I need to check if the username exist ONLY if has a diferent ID, because this is a control panel to manage all users of a system.
So I need to deal with the user_id[] value of the hidden input.

Thanks




Theme © iAndrew 2016 - Forum software by © MyBB