EllisLab text mark
Advanced Search
1 of 2
1
   
Form validation error message issues
Posted: 04 October 2012 01:47 PM   [ Ignore ]
Joined: 2012-10-03
49 posts

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:

<?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($dataset_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($dataset_value('sha1(password_login)'));
    echo 
form_submit('submit_login''Login');
    echo 
form_close();
    
?> 

CONTROLLER:

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);
  
}
 
 } 
 
Posted: 04 October 2012 04:57 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-02
85 posts

Try to take this line out “is_unique[users.email]”

Tell us what happens.

Cheers!

 
Posted: 04 October 2012 07:22 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3800 posts

Your email field is named “email”, but you are doing:

echo form_error('email_login'); 
 Signature 
 
Posted: 05 October 2012 03:38 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-10-03
49 posts

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:

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($dataset_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($dataset_value('sha1(password_login)'));
echo 
form_submit('submit_login''Login');
echo 
form_close(); 
 
Posted: 05 October 2012 04:09 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-02
85 posts

dude, edit it and use [ code ][/ code ]  sorry haha smile

 
Posted: 05 October 2012 04:13 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2012-10-03
49 posts

.

 
Posted: 05 October 2012 07:24 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2012-10-03
49 posts

there ya go, my bad sorry :\

 
Posted: 06 October 2012 12:12 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-02
85 posts

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.

$data = array( 'name' => 'email''class' => 'input''placeholder' => 'Email');
echo 
form_input($dataset_value('email_login')); 

Also in the controller you are referencing as “email_login” instead of just “email”

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

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!

 
Posted: 07 October 2012 02:17 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2012-10-03
49 posts

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:

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);
  
}
 
 } 
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($dataset_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($dataset_value('sha1(password_login)'));
 echo 
form_submit('submit_login''Login');
 echo 
form_close(); 
 
Posted: 07 October 2012 06:39 PM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-02
85 posts
ojcarga - 06 October 2012 12:12 AM
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
   

 

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

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.

 
Posted: 08 October 2012 05:32 PM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Joined: 2012-10-03
49 posts

Ok - First off I want to thank you for all of your time in this matter, and secondly I’m getting sadder by the minute because I’m so close and just not understanding what the problem is.

I have taken your advice on restructuring based off of the user guide, however some errors are posting as they should:

when you submit the form with out touching any fields, it runs the correct errors, and when you enter a correct email and don’t touch pw field, it runs the correct error but when you put in incorrect information with any other incorrect information it loads a white page

I have used the profiler on those pages and the query is running correctly and the fields are being populated correctly and I can log in with out a problem when the correct info is sent.

This is what I have right now:

<?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_login''class' => 'input''placeholder' => 'Email');
echo 
form_input($dataset_value('email_login'));
echo 
"<span class='errors_login'>";
echo 
form_error('password_login');
echo 
form_label('''Password;''password_login');
$data = array( 'name' => 'password_login''class' => 'input''placeholder' => 'Password');
echo 
form_submit('submit_login''Login');
echo 
form_close();
?> 
function validate_credentials_login()
  
{
   
   $this
->load->helper(array('form','url'));
   
$this->load->library('session');
   
$this->load->model('user_model''um');
   
$this->load->library('encrypt');
   
$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
   
   
{
    
    
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;
     
     
}
    }
   }
  
  }
 
 } 
 
Posted: 08 October 2012 06:33 PM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Joined: 2012-10-03
49 posts

since that last post, I’ve gone ahead and put the ->run() INSIDE of the $if($login) which makes more sense, however everything still seems to be sensitive to whether or not the input fields are filled ( they error correctly if not touched ) and getting these NEW errors on an all white page:

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 1

Filename: libraries/Form_validation.php

Line Number: 953

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /Users/michaelsanger/Sites/cl_ci_new/system/core/Exceptions.php:185)

Filename: core/Common.php

Line Number: 442

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘WHERE ` = ‘RalphLeMouf@gmail.com’ LIMIT 1’ at line 2

SELECT * WHERE ` = ‘RalphLeMouf@gmail.com’ LIMIT 1

Filename: libraries/Form_validation.php

Line Number: 954

 
Posted: 08 October 2012 09:51 PM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-02
85 posts

Dude, I guess you are not focusing in all ways you code can be executed.

Put attention to the three lines where it says “ELSE{ /* ........ */ }”

if ($this->form_validation->run() == FALSE)
        
{
            $data[
'main_content''home/home_page';
            
$this->load->view('includes/templates/home_page_template'$data);
        
}
        
else
        
{    
            
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;
                    
}ELSE{ /* ........ */ }
                }ELSE{ 
/* ....... */ }
            }ELSE{ 
/* ........ */ }
        } 

What happen if your code drop there?? a white screen will be shown.

 
Posted: 09 October 2012 12:56 AM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Joined: 2012-10-03
49 posts

I’m still very new to code-igniter. Your suggesting that those last three “}” are defaulted “elses” and I’m just leaving them blank?

 
Posted: 09 October 2012 01:08 PM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-02
85 posts

Yes, add something like this for you to know when (with which submited data) are you getting what.

}ELSE{ echo "First ELSE"}    
}ELSE{ 
echo "Second ELSE"}    
}ELSE{ 
echo "Third ELSE"

 

 
Posted: 11 October 2012 12:08 PM   [ Ignore ]   [ # 15 ]   [ Rating: 0 ]
Joined: 2012-10-03
49 posts

Ok -

So I’ve restructured my logic scheme a bit and heeded all of your advice and STILL having trouble getting this to work correctly. I’ve gone and commented out every step Im’ doing so you can see the logic that I THINK I am using.

As far as the said “elses” what I have seems to account for everything but for some reason it is not.

function validate_credentials_login()
  
{
   
// LOAD THE SESSION LIBRARY
   
$this->load->library('session');
   
// LOAD THE URL AND FORM HELPERS
   
$this->load->helper(array('form','url'));
   
// LOAD THE RELEVENT MODEL AND SET A NAME FOR IT
   
$this->load->model('user_model''um');
   
// LOAD ENCRYTPION LIBRARY IN ORDER TO ENCRYPT PASSWORDS PROPERLY
   
$this->load->library('encrypt');
   
// LOAD THE FORM VALIDATION LIBARARY TO MAKE USE OF ERROR HANDLING
   
$this->load->library('form_validation');
   
// SET RULES FOR MY EMAIL FIELD 
   
$this->form_validation->set_rules('email_login''Email''trim|required');
   
// SET RULES FOR MY PASSWORD FIELD
   
$this->form_validation->set_rules('password_login''Password''trim|required');
   
// MAKE A VARIABLE FOR MY SUBMIT BUTTON
   
$login $this->input->post('submit_login');
   
   
// IF THE SUBMIT BUTTON IS SET
   
if($login{
   
   
// MAKE THIS VARIABLE THAT CHECKS THE EMAIL FEILD INSERTED VIA POST AGAINST THE ONE STORED IN MY DATABASE 
   
$user $this->um->validate_home_login(array('email' => $this->input->post('email_login')));

   
// IF THIS USER EXISTS AND THERE ARE NO ERRORS SET OFF BY THE FORM VALIDATION CHECK
    
if($user && $this->form_validation->run()) {
     
// DO THIS STUFF AKA IF THE USERS PASSWORD IS THE SAME AS THE ONE INSERTED VIA POST AND THE USERS EMAIL IS THE SAME INSERTED VIA POST EVERYTHING IS GOOD AND YOU CAN LOG THEM IN AND START A SESSION
      
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);
      
}
      
      
// IF ANYTHING IS OFF OR DOESN'T MATCH ( SUPOSEDELY ) RUN THE FORM VALIDATION AS FALSE AND RELAOD THE PAGE WITH ERRORS
      
elseif($this->form_validation->run() == FALSE)
      
{
       $data[
'main_content''home/home_page';
       
$this->load->view('includes/templates/home_page_template'$data);
      
}
     } 
    } 
<?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_login''class' => 'input''placeholder' => 'Email');
    echo 
form_input($dataset_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($dataset_value('sha1(password_login)'));
    echo 
form_submit('submit_login''Login');
    echo 
form_close();
    
?> 
function validate_home_login($data)
 
{
  
// TAKING THE DATA FROM THE MODEL AND CHECKING IT AGAINST THE STORED INFO IN THE DB
  
$query $this->db->where($data)->get('users''1');
  
  if(
$query->row())
  
{
   
return $query->row();
  
}
 } 
 
1 of 2
1