【问题标题】:Programmatically Make "$this->form_validation->run()" Return false以编程方式使“$this->form_validation->run()”返回 false
【发布时间】:2023-03-31 05:31:01
【问题描述】:

我的目标包括两个步骤

第 1 步

  • 首先检查空的username AND password
  • 如果为空,则返回登录表单,说明空值 被发现

第 2 步

  • 没有找到空值,所以通过查询数据库检查登录是否有效
  • 如果有效,请转到仪表板
  • 如果没有,返回登录表单,说明登录失败

对于上述场景,我的控制器代码如下:

控制器

$this->form_validation->set_rules('userid', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if ($this->form_validation->run() == FALSE)
{
    $this->load->view('login'); // this works fine if either of one is empty
}
else
{
    // I query the database and check if valid or not
    if(<not_valid>)
    {
       // but how to pass the "Login Failed" message in this code block?
       /* I'm assuming that if I somehow make "$this->form_validation->run()" 
          return false, then the message can be passed to the view */
       $this->load->view('login');
    }
    else
    {
        $this->load->view('dashboard');
    }
}

HTML

echo validation_errors();
<div class="form-group">
    <label for="userid" class="control-label">Enter User ID</label>
    <input type="text" id="userid" name="userid" 
       value="<?php echo set_value('userid'); ?>" class="form-control" 
       placeholder="Email Address"/>
</div>

<div class="form-group">
    <label for="password" class="control-label">Enter Password</label>
    <input type="password" id="password" name="password" class="form-control" 
       placeholder="Secret Password"/>
</div>

PS

我知道回调,但是如何为两个字段(用户名和密码)触发一个回调?如果我这样做:

$this->form_validation->set_rules('userid', 'Username', 'callback_check_valid');
$this->form_validation->set_rules('password', 'Password', 'callback_check_valid');

这是否意味着我必须自己手动检查两个字段是否为空?像这样的东西:?

public function check_valid($un, $pw)
{
    if(trim($un) == '' || trim($pw) == '')
    {
       return false;    
    }
    else
    {
        // check for valid login
    }
}

【问题讨论】:

  • 你需要改变你的逻辑,似乎codeigniters回调无法发送2个参数......但你可以使用$_POST ;)
  • @Kyslik 事实证明,我可以将两个参数发送到回调函数check_valid() :)

标签: php codeigniter validation


【解决方案1】:
public function check_valid() {
    $un = $_POST['username'];
    $pw = $_POST['password'];
    if(trim($un) == '' || trim($pw) == '') {
        return false; 
    } else {
        // check for valid login
    }
}

试试这个逻辑

【讨论】:

  • 这部分我很熟悉。我猜你错过了我指定的部分,我不确定在登录检查失败的情况下如何传递错误消息。
【解决方案2】:

试试这个:

if($this->input->post()){
    $this->form_validation->set_rules('userid', 'Username', 'trim|required');
    $this->form_validation->set_rules('password', 'Password', 'trim|required');

    if ($this->form_validation->run()){
        $username = $this->input->post('userid');
        $password = $this->input->post('password');

        if($this->check_valid($username, $password)){
            $this->load->view('dashboard');
        }else{
            $this->load->view('login');
        }
    }else{
        $this->load->view('login');
    }
}

这与我使用 codeigniter 进行登录检查的方式类似。

您可以将第二次修剪检查从 check_valid 函数中移开,值已被修剪。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
  • 2013-01-13
  • 2021-09-05
相关资源
最近更新 更多