php 表单验证期间带有多个参数的 Codeigniter 中的回调函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19324420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:16:26  来源:igfitidea点击:

Callback function in Codeigniter with multiple parameters during form validation

phpcodeignitervalidationcallback

提问by Kamran Ahmed

In Codeigniter:

在代码点火器中:

Here is the callback function that I am using for validation:

这是我用于验证的回调函数:

public function has_match($password, $username){
    if (0) {
        // user exists
        return true;
    }
    else {
        $this->form_validation->set_message('has_match', 'Invalid Username/password entered ' . $password . ' ' . $username);
        return false;
    }
}

Below are the validation rules:

以下是验证规则:

$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required|callback_has_match[username]');

Can any one please tell me what I am doing wrong here in calling the callback function, as I am unable to get the value of the username field and it keeps showing 'username' (inside the variable $username in callback) instead?

任何人都可以告诉我我在调用回调函数时做错了什么,因为我无法获取用户名字段的值并且它一直显示“用户名”(在回调中的变量 $username 内)?

回答by Petre P?tra?c

Your code is working as expected. You're basically always calling the callback method has_match with the stringusernameas a parameter. I think that you expect that this translates into:

您的代码按预期工作。您基本上总是使用字符串用户名作为参数调用回调方法 has_match 。我认为您希望这会转化为:

callback_has_match[$username]

Therefore, when you access the has_match() method, you would have access to the value of $username. This is however, not the way callback methods work. The parameter that you set in there is a string, which is hardcoded, exactly like you do when you add a rule for min_length[10] - it's not the value of a PHP variable. An easy fix, which I haven't tested but suspect works is to do:

因此,当您访问 has_match() 方法时,您将可以访问$username的值。然而,这不是回调方法的工作方式。您在其中设置的参数是一个硬编码的字符串,与您为 min_length[10] 添加规则时所做的完全一样 - 它不是 PHP 变量的值。一个简单的修复方法,我还没有测试过,但可疑的工作是:

$this->form_validation->set_rules('password', 'Password', 'required|callback_has_match[' . $username . ']');

However the code above is not clean, and seems bad design in my opinion.

然而,上面的代码并不干净,在我看来似乎设计很糟糕。

Now that we've found the problem with the code, I know it's outside the scope of the question, but I would like to point it out - I find it's more of a design issue here. Why do you want to check for the username/password pair inside of a callback to the password field?

现在我们已经找到了代码的问题,我知道它超出了问题的范围,但我想指出它 - 我发现这更像是一个设计问题。为什么要在密码字段的回调中检查用户名/密码对?

Remember, you're validating a form, you shouldn't mix it up with model work. The form doesn't care if the provided username/password combo is correct, it should just look at whether both the fields have been provided correctly, and if so, it should do something about it.

请记住,您正在验证表单,不应将其与模型工作混为一谈。表单并不关心所提供的用户名/密码组合是否正确,它应该只查看两个字段是否都已正确提供,如果是,则应该对其进行处理。

I would adapt your code above to:

我会将您上面的代码调整为:

$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required|callback_has_match[username]');

if ($this->form_validation->run() != FALSE) {
    $validLogin = $this->muser->checkLogin($username, $password);

    if ($validLogin) {
        //Username/password combo exists in DB, save in session or whatever.
    } else {
        //Username/password combo does not exist in DB, show an error.
    }
} else {
    //Code for when form fields have not been provided correctly.
}

回答by Muhammad Raheel

Instead of sending parameters with call back you can always get them with post

您可以随时通过 post 获取参数,而不是通过回调发送参数

Here is your call back

这是您的回电

public function has_match(){
    if (0) {
        // user exists
        return true;
    }
    else {

        $password   =   $this->input->post('password');
        $username   =   $this->input->post('username');

        $this->form_validation->set_message('has_match', 'Invalid Username/password entered ' . $password . ' ' . $username);
        return false;
    }
}

回答by sokhy

// CALLBACKS

// 回调

public function check_user(){

公共函数 check_user(){

    $result = FALSE;

    $username=$this->input->post('username');

    $email=$this->input->post('emailad');

    $dbmember=$this->members->get_members();

    foreach ( $dbmember as $key){

        // condition of username and email  
        if($username==$key && $email==$key){

        if($username == $key->UserName && $email == $key->EmailAddress){
              $this->form_validation->set_message('check_user','already existed!
                      Please check username and email agian.');
              return FALSE;
              break;
            }                

        return TRUE;

        }  
    }