EllisLab text mark
Advanced Search
2 of 2
2
   
Form validation error message issues
Posted: 11 October 2012 12:54 PM   [ Ignore ]   [ # 16 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-02
85 posts

What error are you getting now? Blank page? or something else?

You have this:

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()  ???

 

 
Posted: 11 October 2012 03:01 PM   [ Ignore ]   [ # 17 ]   [ Rating: 0 ]
Joined: 2012-10-03
49 posts

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:

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

elseif($this->form_validation->run() { successwhich I thought I am already doing???????

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

// 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 
 
Posted: 11 October 2012 03:51 PM   [ Ignore ]   [ # 18 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-02
85 posts

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.

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. cool smile

Cheers!

 
Posted: 11 October 2012 04:58 PM   [ Ignore ]   [ # 19 ]   [ Rating: 0 ]
Joined: 2012-10-03
49 posts

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

else
   
{
    $this
->form_error();
   
 
Posted: 11 October 2012 05:23 PM   [ Ignore ]   [ # 20 ]   [ Rating: 0 ]
Joined: 2012-10-03
49 posts

I’m really close but the errors I tried to add via

echo validation_error(); 

is not correct. So should I make that from scratch or is there a code-igniter shortcut for that?

This is what I ended up with:

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
       
{
         
echo validation_error();
       
}
      }
      
else
      
{
       
echo validation_error();
      
}
    } 
 
Posted: 16 October 2012 12:59 AM   [ Ignore ]   [ # 21 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-02
85 posts

I meant you can do something like this:

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
    
{
      $data[
"my_custom_error""Password you entered and the one in the DB donĀ“t match!!";
    
}
   }
   
else
   
{
    $data[
"my_custom_error""NO $user, validation of validate_home_login failed.";
   
}
  }
    
  $data[
'main_content''home/home_page';
  
$this->load->view('includes/templates/home_page_template'$data);   
  
    

OR, maybe you want to use the form validation library, under “Setting Error Messages” subtitle you have the way to achieve that:
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#settingerrors

Cheers!

 
2 of 2
2