0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I made a lot a test but now I understand :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.