Codeigniter在回调时传递两个值(form_validation)

时间:2022-01-11 07:07:28

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

这是我的代码http://pastebin.com/JKnTEqLN

3 个解决方案

#1


3  

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

如果要将两个变量传递给函数,请更改第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

对于两个以上的值,您需要使用http_build_query($ val2)然后在函数内部展开它

#2


1  

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

而不是使用_check_login()函数回调作为用户名字段的验证器(这真的没有意义,imho),为什么不在输入字段被验证时调用check_login($ username,$ password)函数?

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.

无论如何,这就是form_validation库的用途,用于验证输入,而不是详细说明数据。

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.

如果您只想检查用户是否存在(例如,为了避免重复)来验证输入字段,那么您不需要$ password参数,因此回调可以正常工作。

#3


0  

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.

public function score_check($ name,$ arr){如果你想传递两个变量,我找到了用两个变量创建数组的最佳方法。

$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

希望有所帮助

#1


3  

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

如果要将两个变量传递给函数,请更改第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

对于两个以上的值,您需要使用http_build_query($ val2)然后在函数内部展开它

#2


1  

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

而不是使用_check_login()函数回调作为用户名字段的验证器(这真的没有意义,imho),为什么不在输入字段被验证时调用check_login($ username,$ password)函数?

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.

无论如何,这就是form_validation库的用途,用于验证输入,而不是详细说明数据。

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.

如果您只想检查用户是否存在(例如,为了避免重复)来验证输入字段,那么您不需要$ password参数,因此回调可以正常工作。

#3


0  

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.

public function score_check($ name,$ arr){如果你想传递两个变量,我找到了用两个变量创建数组的最佳方法。

$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

希望有所帮助