开发者

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?

funct开发者_JS百科ion($val1, $val2)

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


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


(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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜