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

#11
[eluser]RalphLeMouf[/eluser]
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:

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_login', 'class' => 'input', 'placeholder' => 'Email');
echo form_input($data, set_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();
?&gt;

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

}

#12
[eluser]RalphLeMouf[/eluser]
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 ` = '[email protected]' LIMIT 1' at line 2

SELECT * WHERE ` = '[email protected]' LIMIT 1

Filename: libraries/Form_validation.php

Line Number: 954

#13
[eluser]ojcarga[/eluser]
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{ /* ........ */ }"

Code:
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.

#14
[eluser]RalphLeMouf[/eluser]
I'm still very new to code-igniter. Your suggesting that those last three "}" are defaulted "elses" and I'm just leaving them blank?

#15
[eluser]ojcarga[/eluser]
Yes, add something like this for you to know when (with which submited data) are you getting what.

Code:
}ELSE{ echo "First ELSE"; }    
}ELSE{ echo "Second ELSE"; }    
}ELSE{ echo "Third ELSE"; }

#16
[eluser]RalphLeMouf[/eluser]
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.

Code:
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);
      }
     }
    }

Code:
&lt;?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($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;

Code:
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();
  }
}

#17
[eluser]ojcarga[/eluser]
What error are you getting now? Blank page? or something else?

You have this:
Code:
if($user && $this->form_validation->run()){
   //.....
}elseif($this->form_validation->run() == FALSE){
    //.....
}

But now, what happen if you have this:

(NOT) $user AND (YES) $this->form_validation->run() ???

#18
[eluser]RalphLeMouf[/eluser]
Ok - thanks again for your great help!

I'm aware of the unspoken 'elses', however I am confused because it seems like my criteria is covered with everything I have. What you just said makes sense though. I think my main problem is knowing what to put in those other 'elses' . I know what to put in plain text, just not in sync with CI.

It would be something like:

Code:
if($user && $this->form_validation->run()){
   //.....
}elseif($this->form_validation->run() == FALSE){
    //.....
}elseif(!user) { run appropriate error}

elseif($this->form_validation->run() { success, which I thought I am already doing???????}

So to answer your question about what the form is doing now with what I currently have:

Code:
// IF I HIT SUBMIT WITH OUT TOUCHING ANY INPUTS IT LOADS PAGE WITH PROPER INVALID EMAIL AND PASSWORD ERRORS

// IF I DON'T REFRESH THE PAGE AND RANDOM TEXT TO THE EMAIL FIELD. IT RELOADS THE PAGE WITH THE BUNK EMAIL TEXT I ENTERED AND NO INVALID EMAIL ERROR BUT A INVALID PASSWORD ERROR

//IF I REFRESH THE PAGE AND PUT A VALID EMAIL AND LEAVE PASSWORD BLANK OR INVALID PASSWORD IT RELOADS THE PAGE WITH THE EMAIL I ENTERED AND  NO ERROR

//IF I ENTER A BOGUS EMAIL THAT IS NOT IN THE DB AND A BOGUS PASSWORD IT LOADS THE PAGE BLANK

#19
[eluser]ojcarga[/eluser]
This thread is getting bigger more than what it should. Let's do it easier, here is the code as I would use it instead of your.
Code:
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
    {
     //HERE U CAN CREATE YOUR OWN ERRORS MESSAGES
    }
   }
   else
   {
    //HERE U CAN CREATE YOUR OWN ERRORS MESSAGES
   }
  }
    
  $data['main_content'] = 'home/home_page';
  $this->load->view('includes/templates/home_page_template', $data);  
  
    }

Try it, compare with yours and comment us back. :coolsmile:

Cheers!

#20
[eluser]RalphLeMouf[/eluser]
makes wonderful sense. I'm gonna give it a go. My one question is when you are telling me to "create my own error messages". Are you saying stray beyond code-igniters form_error() and make my own custom PHP error message system? Or would it be something like
Code:
else
   {
    $this->form_error();
   }


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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