S7_0
S7_0

Reputation: 1183

CodeIgniter: Why variable change after callback function? Am I using it the right way?

I'm currently using CodeIgniter. And after a form validation I used the function "set_rules" for check if the user's information is correct. Otherwise, I tried to send 2 variable using the "callback" function but it seem that the second variable change its value when I use "callback" function. If in my form I fil it as:

Username = "test_username"    
Password = "test_password"   

In my function database,

$username will display "test_username"       
$password will display "test_username,test_password".     

I tried by this way:

 function index()
 {
  $this->load->library('form_validation');
  $username = $this->input->post('username');
  $password = $this->input->post('password');
  $this->form_validation->set_rules('username', 'Username', 'trim|required', 'wrong or missing username');
  $this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database($username, $password)', 'wrong or missing password');
 }

  function check_database()
  {
    echo '$password'. '</br>'; //display => test_username
    echo '$username'. '</br>'; // display => test_username,test_password
  }

I tried to replace a few line of the higher code by:

 function index()
 {
  $this->load->library('form_validation');
  $this->form_validation->set_rules('username', 'Username', 'trim|required', 'wrong or missing username');
  $this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database['. $this->input->post($username). ','. $this->input->post($password), 'wrong or missing password'];

  function check_database($password, $username)
  {
    echo '$password'. '</br>'; //display => test_username
    echo '$username'. '</br>'; // display => test_username,test_password
  }

But it's the same problem. I didn't find the manual of the callback function on the CodeIgniter site. The second question I have is when I write

  $this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database', 'wrong or missing password');

  function check_database() //work only if I write check_database($password)
  {
    //blah blah blah
  }

It pop me an error. Given I didn't find any manual of call_back function, I suppose that the callback function is use into a set_rules which is testing the password variable so I think that the call_back function will automatically sent the password variable to check_database() function.(That's why I need to put $password to the check_database prototype).

I've already find a solution but I'm just here for know what happen(I'm curious) ?

Does anyone can tell me why in the first and the second code, the second parameter of callback change once it is on the check_database() ? And by the way can you confirm me if am I right for the last code ? More precisely when I say that the call_back function will automatically sent the password variable to check_database() ?

Thank's

PS: In the code I show you before, I voluntarily remove a part of the code to avoid you to read to much because I post is a bit longer I think.

Upvotes: 0

Views: 330

Answers (1)

Jackie
Jackie

Reputation: 494

The variable or value does not change. In codeigniter form validation, the callback first parameter supply the value.

$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database[x]');

....

function check_database($str, $param1)
{
     echo $str;   // password
     echo $param1; // x
}

If you wish to supply other input post param, this is easier:

$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');

function check_database($str)
{
     $username = $this->input->post('username'); // same input post value
     ....
}

Hope this helps.

http://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods

Upvotes: 1

Related Questions