【问题标题】:Display a separate error message显示单独的错误消息
【发布时间】:2016-12-29 14:42:47
【问题描述】:

当用户尝试在未填写任何信息的情况下登录时,我想显示消息您缺少输入的用户名或密码。

目前我使用的验证方法只有在用户名或密码与数据库记录不匹配时才会显示错误。

如何同时使用?

public function validate() {

    $this->load->library('user');

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

    if (!isset($username) || !isset($password) || !$this->user->login($username, $password)) {
        $this->error['warning'] = 'The login information is incorrect!';
    }

    return !$this->error;
}

【问题讨论】:

  • 您想显示用户名和密码不匹配的错误吗?

标签: php codeigniter


【解决方案1】:
public function validate() {

    $this->load->library('user');

    $username = $this->input->post('username');
    $password = $this->input->post('password');
if($username!='' && $password!='' ){
    if (!isset($username) || !isset($password) || !$this->user->login($username, $password)) {
        $this->error['warning'] = 'The login information is incorrect!';
    }
}else{
   $this->error['warning'] = 'Information incorrect!';
}
    return !$this->error;
}

Ps:-但是最好用javascript处理客户端这样的错误,因为它可能会增加服务器的负载:-)

【讨论】:

  • 编码愉快:-)
【解决方案2】:

也许尝试将 if 条件分成两个条件?

public function validate() {

    $this->load->library('user');

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

    if (!isset($username) || !isset($password)){
        $this->error['warning'] = 'You have missing input Username or Password';
    }elseif(!$this->user->login($username, $password)) {
        $this->error['warning'] = 'The login information is incorrect!';
    }

    return !$this->error;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 2020-11-18
    • 2013-09-27
    • 2020-10-07
    • 1970-01-01
    • 2011-03-12
    • 2014-09-11
    相关资源
    最近更新 更多