Welcome Guest, Not a member yet? Register   Sign In
Validation Custom Error Message Clarification
#1

[eluser]escape[/eluser]
I've got form validation working with the following abbreviated code from my controller:

Code:
$rules['username'] = "required|min_length[4]|max_length[12]";
$rules['password'] = "required|min_length[4]|max_length[12]";
        
$this->validation->set_rules($rules);        

$fields['username'] = 'Username';
$fields['password'] = 'Password';
        
$this->validation->set_fields($fields);

if ($this->validation->run() == FALSE)
{
    // display current view with validation error messages
}
else
{
    // check for successful login
    if($this->login($_POST['username'],$_POST['password'])
    {
       // successful login redirect user
       redirect('successfulLoginPage');
    }
    else
    {
      // login failed display error message to user via validation function
      //  $this->validation->set_message('invalidLogin', 'Login Error Msg.');    
      // display current view with login error messages
    }
}

If the user passes the validation rules but fails to login correctly I wish to set a custom validation message and display it to the user (separate from the username & password fields). However I'm not at all clear on how to proceed using the $this->validation->set_message function.

In my view I'm assuming I may need something like:
Code:
<?php echo $this->validation->invalidLogin_error; ?>

I've looked through many of the validation posts here but I'm still missing some key concept. BTW: I've seen suggestions to use $data[] as an alternate approache but I somehow feel this can be accomplished with validaton.

Any guidance would be appreciated.

Thanks
Chris
#2

[eluser]OES[/eluser]
I would suggest using Flash Data.

IE example of my validation on a working site. I keep my validation within a seperate function but you can easy keep it withing the same function and not do a redirect.

Code:
rules = array( // Your rules );
$fields  =  array(  // Your Fileds  );

$this->validation->set_fields($fields);            
$this->validation->set_rules($rules);

// PROCESS FORM
$data = $this->input->post('input');
if ($this->validation->run() == FALSE):
  $this->session->set_flashdata('formfields', $this->input->post('input'));
  $this->session->set_flashdata('errors', $this->validation->error_string);
  redirect('admin/onsite/add_onsite/');
else:
  if ( $this->Onsite_model->insert_onsite( $this->input->post('input'))):
    $this->session->set_flashdata('success', array('Success, Data Added, Bla bla'));
    redirect('admin/onsite/add_onsite/');
  else:
    $this->session->set_flashdata('formfields', $this->input->post('input'));
    $this->session->set_flashdata('errors', array('Your own personal message about the error'));
    redirect('admin/onsite/add_onsite/');
  endif;
endif;

Then in my controller I collect any flash data and pass it to the view ie.

Code:
$this->data['success'] = $this->session->flashdata('success');
$this->data['fields'] = $this->session->flashdata('formfields');
$this->data['errors'] = $this->session->flashdata('errors');

I can then repopulate my form and display errors/success etc.

See userguide: http://ellislab.com/codeigniter/user-gui...sions.html

Hope this helps.

Lee




Theme © iAndrew 2016 - Forum software by © MyBB