简体   繁体   中英

Codeigniter form validation rules order

I've set this rules on the controler

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;
        }
    }

and the view

<div class="container">
    <div class="col-md-4">
        <?php $this->load->helper('form'); ?>
        <?php echo form_open('checkLogin'); ?>
        <?php echo form_error('bad_login'); ?>
        <?php echo form_error('username'); ?>
        <div class="form-group">
            <?php echo form_label('Usuario:'); ?>
            <?php echo form_input(array('id' => 'username', 'name' => 'username', 'class'=>'form-control', 'placeholder'=>"Nombre de usuario")); ?>
        </div>
        <?php echo form_error('pass'); ?>
        <div class="form-group">
            <?php echo form_label('Contraseña:'); ?>
            <?php echo form_password(array('id' => 'pass', 'name' => 'pass', 'class'=>'form-control', 'placeholder'=>"Contraseña")); ?>
        </div>
        <?php echo form_submit(array('id' => 'submit', 'value' => 'Enviar', 'class'=>'btn btn-primary')); ?>
        <?php echo form_close(); ?>
    </div>
</div>

It's a login form with and input to the user and another to the password , but if the pass or the username are empty there is no need to check if pass and the username are correct(and show the message error), I'm not sure how to do that

You can simply do this:

$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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM