Dexty
Dexty

Reputation: 1472

Codeigniter passing two values on callback (form_validation)

I'm trying to pass two values to a function, so that i can check the username and password for the users. However, i can't get it to work. When i have one value, there is ne problem doing this. But how can i pass more than one value?

function($val1, $val2)

This is my code http://pastebin.com/JKnTEqLN

Upvotes: 2

Views: 2234

Answers (3)

aedwards
aedwards

Reputation: 391

public function score_check($name, $arr) {If you would like to pass two variables I have found the best way to create an array with both variables.

$arr = array($val1, $val2);
$this->form_validation->set_rules('username', 'Username', 'required|alpha|xss_clean|callback__check_login['.$arr.']');

Your callback would be something like

public function check_login($str, $arr) {
      $val1  = $arr[0];
      $val2 = $arr[1];
}

Hope that helps

Upvotes: 0

Dave Kiss
Dave Kiss

Reputation: 10485

If you want to pass two variables to your function, change line 6

$this->form_validation->set_rules('username', 'Username', 'required|alpha|xss_clean|callback__check_login');

to

$this->form_validation->set_rules('username', 'Username', 'required|alpha|xss_clean|callback__check_login[$val1,$val2]');

For more than two values, youll need to use http_build_query($val2) and then explode it inside of your function

Upvotes: 3

Damien Pirsy
Damien Pirsy

Reputation: 25435

(please correct me if I didn't get the question right:)

I wouldn't do that way.

Instead of using the _check_login() function callback as a validator for the username field (which doesn't really make sense, imho), why not call the check_login($username,$password) function WHEN the input fields are validated?

So

  if($this->form_validation->run() == FALSE)
  {
   $this->load->view('user_login');
  }
  else
  {
    $this->user_model->check_login();
  }

In User model you will be doing the check

Anyway, that's what the form_validation library is for, to validate inputs, not to elaborate datas.

If you just wanna check the existence of a user (to avoid duplicates, for example) to validate the input field, then you don't need the $password parameter, so the callback would work just fine.

Upvotes: 1

Related Questions