codeigniter simple login validation not working

2019-09-03 19:41发布

问题:

Im a beginner in web programming, so please bear with me.

I tried to create a login system that will do those validation :

  1. The email is required and must be valid
  2. The password is required
  3. If the login failed, i will create a span for the user

I can accomplish the 1st & 2nd easily using codeigniter's form validation. However, the 3rd is not that easy because i need to check it first from the DB.

So i tried to create a code like this :

function login() {
        $data['validation'] = TRUE;

        $this->form_validation->set_rules('txt_email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('txt_password', 'Password', 'required');

        if($this->form_validation->run() !== FALSE)
        {
            $log_in = $this->umat_m->log_in($this->input->post('txt_email'), $this->input->post('txt_password'));
            if($log_in !== FALSE)
            {
                $data['validation'] = TRUE;
                $_SESSION['username'] = $this->input->post('txt_email');
                redirect('backend/umat/index');
            }

            else
                $data['validation'] = FALSE;
        }

        $this->load->view('backend/login_v', $data);
    }

And the HTML :

<?php echo form_submit('btn_submit', 'Log In', 'class = "btn btn-primary"'); ?> <br/>
                        <?php
                            if($validation === FALSE)
                                echo '<span class="label label-important">Wrong Username/Password</span>';
                            else
                                echo '<span class="label label-important"><?php echo validation_errors(); ?></span>';
                        ?>

What i tried to do is if the user already fill the Email & Password field but the value is wrong then i will echo the first span.

However, this code is still not working. When the user fill the wrong value (but not empty), the Wrong Username/Password span is echoed but when the textfield is empty, the codeigniter's form validation dont show any message (that means the 2nd echo is never showed).

Any help is appreciated, and please just ask me if you need something more.

回答1:

It is better to handle this in Controller:

Controller:

function login()
{
    // Set the default value
    $data['error'] = '';

    $this->form_validation->set_rules('txt_email', 'Email', 'required|valid_email');
    $this->form_validation->set_rules('txt_password', 'Password', 'required');

    if($this->form_validation->run() !== FALSE)
    {
        $log_in = $this->umat_m->log_in(
            $this->input->post('txt_email'),
            $this->input->post('txt_password')
        );

        if($log_in !== FALSE)
        {
            $_SESSION['username'] = $this->input->post('txt_email');
            redirect('backend/umat/index');
        } else {
            // Set your error message
            $data['error'] = 'Wrong Username/Password';
        }
    } else {
        // Set the validation errors
        $data['error'] = validation_errors();
    }

    $this->load->view('backend/login_v', $data);
}

View:

<?php if ($error): ?>
    <span class="label label-important"><?php echo $error; ?></span>
<?php endif ?>


回答2:

for checking each field validation errors inside your view do write like this after each input which you are going to validate it.

 <input type='text' name='id_no' id='id_no' />
 <?=form_error('id_no')?>