简体   繁体   中英

How do I echo a form error in a else statement in my controller in code-igniter?

I have all of the different combinations of submitting a incorrect form for my login form in code-igniter working as they should.

**EDIT - This seems like the most logical formation as the other suggestions were loading double views and or not cohesive with my structure. All of the errors work properly except one.

When you hit submit with CORRECT email and gibberish password, no error appears and the page reloads with the entered email auto filled and the password field set back to the defaulted "PASSWORD" text.

I have been stuck on this far too and long and any help would be greatly appreciated.

function validate_credentials_login()
    {
        $this->load->library('session');
        $this->load->helper(array('form','url'));
        $this->load->model('user_model', 'um');
        $this->load->library('encrypt');
        $this->load->library('form_validation');


        $this->form_validation->set_rules('email_login', 'Email', 'trim|required|valid_email'); 
        $this->form_validation->set_rules('password_login', 'Password', 'trim|required');


        if ( $this->form_validation->run() === TRUE ) 
        {   
            $user = $this->um->validate_home_login(array('email' => $this->input->post('email_login')));

            if ( $user )
            {
                if ( $user->password == $this->encrypt->sha1( $user->salt .         $this->encrypt->sha1($this->input->post('password_login'))) && $user->email == $this->input->post('email_login') ) 
                {
                    $this->session->set_userdata(array('email' => $this->input->post('email_login')));
                    redirect('account/edit');
                }
                else
                {
                   $this->form_validation->run() == FALSE;
                }
            }
            else
            {
                $this->form_validation->run() == FALSE;
            }
        }

  $data['main_content'] = 'home/home_page';
  $this->load->view('includes/templates/home_page_template', $data);   

    } 

What I would do (and have done) is just use validation_errors() on the login view page. You can do that by doing this:

if ($user)
    if ($success)
    {
        redirect(...);
    }
}
$this->load->view('login_page_template', $data); // Not sure what your login view is named

and in the login view:

...
<?php echo validation_errors('<div class="errors_login">', '</div>'); ?>
...

If you change your logic slightly, you could get rid of the else 's and just load the login view after the if statements run. If the user successfully logs in, they will be redirected before the login view gets loaded anyway. This way, you don't have to else every if statement and duplicate the same code over and over. Let me know if that doesn't make sense.

Edit: like @m4t1t0 said, you don't have to pass the errors to the view, you can just echo validation_errors('<div class="errors_login">', '</div>'); wherever you want the errors to show up. I've updated my example to reflect this.

If you want to show errors individually, above each form item (ie email does not exist, password incorrect, etc) then you can use echo form_error('field_name'); in each respective location.

Edit 2: I also just noticed you're using sha1 encryption for your passwords. This is fine for a low-security setup, but I would recommend using PHP's crypt() function, which by default uses the Blowfish algorithm (if available). Here's a basic implementation:

// to make password hash
$password = crypt($this->input->post('password'));

// to check password hash
if (crypt($this->input->post('password'), $password_from_db) == $password_from_db)
{
    // success 
}

As @Brendan says, you don't need the last 2 else statement, but you even not need to pass the errors to the view! you can put this code:

In the controller:

$this->form_validation->set_rules('email_login', 'Email', 'trim|required|valid_email'); 
$this->form_validation->set_rules('password_login', 'Password', 'trim|required');


if ( $this->form_validation->run() === TRUE ) 
{   
  $user = $this->um->validate_home_login(array('email' => $this->input->post('email_login')));

  if ( $user )
    {
    if ( $user->password == $this->encrypt->sha1( $user->salt .         $this->encrypt->sha1($this->input->post('password_login'))) && $user->email == $this->input->post('email_login') ) 
      {
        $this->session->set_userdata(array('email' => $this->input->post('email_login')));
        redirect('account/edit');
      }


  $data['main_content'] = 'home/home_page';
  $this->load->view('includes/templates/home_page_template', $data);   
}

And in the view:

<?php echo validation_errors('<div class="error">', '</div>'); ?>

The <div class="error" only is showed if there are errors validating the form, in other case nothing is showed.

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