Codeigniter表单验证规则顺序

时间:2019-06-04 13:52:51

标签: php codeigniter-3

我已经在控制器上设置了此规则

# .gitignore file sample directive
vendor/

和视图

public function checkLogin()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules(
            'username', 
            'nombre de usuario', 
            'required',
            array('required' => 'Debes introducir un  %s.')
        );
        $this->form_validation->set_rules(
            'pass',
            'contraseña',
            'required',
            array('required' => 'Debes introducir una  %s.')
        );
        $this->form_validation->set_rules(
            'bad_login',
            'login',
            'callback_checkCredentials'
        );

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('header');
            $this->load->view('login/login_view');
            $this->load->view('footer');
        } else {
            $this->session->set_userdata('logged',true);
            $this->session->set_userdata('user',$this->input->post('username'));
            redirect('main');
        }
    }

    public function checkCredentials(){
        if($this->input->post('username') == $this->user &&  $this->input->post('pass') == $this->pass){

            return true;
        }else{
            $this->form_validation->set_message('checkCredentials', 'El usuario/contraseña introducido no es correcto');
            return false;
        }
    }

这是一个登录表单,有用户输入,另一个输入密码,但是如果密码或用户名为空,则无需检查密码和用户名是否正确(并显示消息错误),我我不确定该怎么做

1 个答案:

答案 0 :(得分:0)

您可以简单地执行以下操作:

$username = trim($this->input->post('usename'));
$password = trim($this->input->post('password'));
if(!empty($username) && !empty($password))
{
    // do you validation
}
else
{
    // maybe show error message
}