• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form validation error message issues

#1
[eluser]RalphLeMouf[/eluser]
Hello -

I have a login form that I'm having trouble getting errors to post properly.

I have a form that has a email and password field. When I submit the form blank, both "invalid email/password" errors post as they should, however with any other combination ( correct email and incorrect password, both incorrect ) the "invalid email" post no matter what and the "invalid password" NEVER post's. Here is my code:

VIEW:

Code:
<?php
    echo form_open('auth/validate_credentials_login');
    echo "<span class='errors_login'>";
           echo form_error('email_login');
    echo "</span>";
           echo form_label('','Email', 'email_login');
    $data = array( 'name' => 'email', 'class' => 'input', 'placeholder' => 'Email');
    echo form_input($data, set_value('email_login'));
           echo "<span class='errors_login'>";
           echo form_error('password_login');
    echo "</span>";
                  echo form_label('', 'Password', 'password_login');
    $data = array( 'name' => 'password_login', 'class' => 'input', 'placeholder' => 'Password');
    echo form_password($data, set_value('sha1(password_login)'));
    echo form_submit('submit_login', 'Login');
    echo form_close();
    ?&gt;

CONTROLLER:

Code:
function validate_credentials_login()
  {
   // WHEN THE VIEW IS LOADED THIS FUNCTION IS CALLED AND LOADS MODEL AS WELL AS DEFINES THE SALT VARIABLE AND LOADS THE ENCRYPTING HELPER LIBRARY
  
   $this->load->library('encrypt');
   $this->load->helper('url');
   $this->load->library('form_validation');
   $this->form_validation->set_rules('email_login', 'Email', 'required|is_unique[users.email]');
   $this->form_validation->set_rules('password_login', 'Password', 'required');
   $this->load->library('session');
   $this->load->model('user_model', 'um');
   $login = $this->input->post('submit_login');
  
  

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

     // CHECK THE USER'S PASSWORD AGAINST THE ONE FROM THE LOGIN FORM
     if($user->password == $this->encrypt->sha1( $user->salt . $this->encrypt->sha1($this->input->post('password_login')))) {
      $this->session->set_userdata(array(
       'email' => $this->input->post('email_login')
      ));
      redirect('account/dashboard');
      exit;
     }
    }
   }
  
   if($this->form_validation->run() == FALSE){
   $data['main_content'] = 'home/home_page';
   $this->load->view('includes/templates/home_page_template', $data);
  }

}

#2
[eluser]ojcarga[/eluser]
Try to take this line out "is_unique[users.email]"

Tell us what happens.

Cheers!

#3
[eluser]CroNiX[/eluser]
Your email field is named "email", but you are doing:
Code:
echo form_error('email_login');

#4
[eluser]RalphLeMouf[/eluser]
okay, so with this current set-up. I'm getting both errors no matter what...meaning when I have a correct email and a false password, both errors pose, as only the password one should. It's saying valid emails are invalid.

Here is what I currently have:


Code:
function validate_credentials_login()
  {
   // WHEN THE VIEW IS LOADED THIS FUNCTION IS CALLED AND LOADS MODEL AS WELL AS DEFINES THE SALT VARIABLE AND LOADS THE ENCRYPTING HELPER LIBRARY
  
   $this->load->library('session');
   $this->load->model('user_model', 'um');
   $this->load->library('encrypt');
   $this->load->helper('url');
   $this->load->library('form_validation');
   $this->form_validation->set_rules('email_login', 'Email', 'required');
   $this->form_validation->set_rules('password_login', 'Password', 'required');
  
   $login = $this->input->post('submit_login');
  
  

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

     // CHECK THE USER'S PASSWORD AGAINST THE ONE FROM THE LOGIN FORM
     if($user->password == $this->encrypt->sha1( $user->salt . $this->encrypt->sha1($this->input->post('password_login')))) {
      $this->session->set_userdata(array(
       'email' => $this->input->post('email_login')
      ));
      redirect('account/dashboard');
      exit;
     }
    }
   }

echo form_open('auth/validate_credentials_login');
echo "<span class='errors_login'>";
echo form_error('email_login');
echo "</span>";
echo form_label('', 'Email', 'email_login');
$data = array( 'name' => 'email', 'class' => 'input', 'placeholder' => 'Email');
echo form_input($data, set_value('email_login'));
echo "<span class='errors_login'>";
echo form_error('password_login');
echo "</span>";
echo form_label('', 'Password;', 'password_login');
$data = array( 'name' => 'password', 'class' => 'input', 'placeholder' => 'Password');
echo form_password($data, set_value('sha1(password_login)'));
echo form_submit('submit_login', 'Login');
echo form_close();

#5
[eluser]ojcarga[/eluser]
dude, edit it and use [ code ][/ code ] sorry haha Smile

#6
[eluser]RalphLeMouf[/eluser]
.

#7
[eluser]RalphLeMouf[/eluser]
there ya go, my bad sorry :\

#8
[eluser]ojcarga[/eluser]
Some stuff I just realized:


1. Your input name is "email" ('name' => 'email') but you are handling it as "email_login". This is what @CroNiX said above. Check this out.
Code:
$data = array( 'name' => 'email', 'class' => 'input', 'placeholder' => 'Email');
echo form_input($data, set_value('email_login'));
Also in the controller you are referencing as "email_login" instead of just "email"
Code:
$this->form_validation->set_rules('email_login', 'Email', 'required|is_unique[users.email]');

2. You are catching "email" as $this->input->post('email_login'), same as previous.

3. I am not sure if that could be problematic but try to structure the run validation method before all the stuff, like this:


Code:
function validate_credentials_login()
  {
   // WHEN THE VIEW IS LOADED THIS FUNCTION IS CALLED AND LOADS MODEL AS WELL AS DEFINES THE SALT VARIABLE AND LOADS THE ENCRYPTING HELPER LIBRARY
  
   $this->load->library('session');
   $this->load->model('user_model', 'um');
   $this->load->library('encrypt');
   $this->load->helper('url');
   $this->load->library('form_validation');
   $this->form_validation->set_rules('email_login', 'Email', 'required');
   $this->form_validation->set_rules('password_login', 'Password', 'required');
  
   $login = $this->input->post('submit_login');
  
  
   if($this->form_validation->run() == FALSE)
   {
      $data['main_content'] = 'home/home_page';
      $this->load->view('includes/templates/home_page_template', $data);
   }
    else
   {
      //success code here
   }
    
      
} //#end validate_credentials_login() method

Cheers!

#9
[eluser]RalphLeMouf[/eluser]
Ok - So I'm ONE step off!

The way I have it set up now, is that it doesn't pose the email and password error from the OTHER form on the page ( which is good and was a problem before ) and poses both errors properly if submitted blank BUT if correct email and INCORRECT password, then it just loads the page all white ( the url at this point is what it should be "http://www.clci.dev/index.php/auth/validate_credentials_login" )

Here is what I have:

Code:
function validate_credentials_login()
  {
   // WHEN THE VIEW IS LOADED THIS FUNCTION IS CALLED AND LOADS MODEL AS WELL AS DEFINES THE SALT VARIABLE AND LOADS THE ENCRYPTING HELPER LIBRARY
  
   $this->load->library('session');
   $this->load->model('user_model', 'um');
   $this->load->library('encrypt');
   $this->load->helper('url');
   $this->load->library('form_validation');
   $this->form_validation->set_rules('email_login', 'Email', 'required');
   $this->form_validation->set_rules('password_login', 'Password', 'required');
  
   $login = $this->input->post('submit_login');
  
  

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

     // CHECK THE USER'S PASSWORD AGAINST THE ONE FROM THE LOGIN FORM
     if($user->password == $this->encrypt->sha1( $user->salt . $this->encrypt->sha1($this->input->post('password_login')))) {
      $this->session->set_userdata(array(
       'email' => $this->input->post('email_login')
      ));
      redirect('account/edit');
      exit;
    
     }
    }
   }
  
   if($this->form_validation->run() == FALSE){
   $data['main_content'] = 'home/home_page';
   $this->load->view('includes/templates/home_page_template', $data);
  }

}

Code:
echo form_open('auth/validate_credentials_login');
echo "<span class='errors_login'>";
echo form_error('email_login');
        echo "</span>";
echo form_label('', 'Email', 'email_login');
$data = array( 'name' => 'email_login', 'class' => 'input', 'placeholder' => 'Email');
echo form_input($data, set_value('email_login'));
echo "<span class='errors_login'>";
echo form_error('password_login');
        echo "</span>";
echo form_label('', 'Password;', 'password_login');
$data = array( 'name' => 'password_login', 'class' => 'input', 'placeholder' => 'Password');
echo form_password($data, set_value('sha1(password_login)'));
echo form_submit('submit_login', 'Login');
echo form_close();

#10
[eluser]ojcarga[/eluser]
[quote author="ojcarga" date="1349496764"]
Code:
if($this->form_validation->run() == FALSE)
   {
      $data['main_content'] = 'home/home_page';
      $this->load->view('includes/templates/home_page_template', $data);
   }
    else
   {
      //success code here
   }

[/quote]

You are not using the form validation as it should. What you are doing right now is to do all stuff regarding the user login before
Code:
if($this->form_validation->run() == FALSE)
so migth be your code never reach to the "run()" method.

I would recommend you to follow step by step the Form Validation Class and re-struct your code accordingly.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.