1
votes

Can I display a single message for the multiple form fields in CodeIgniter? For example, I have set following rules for the email and password fields. I want to display only one message if any of these two fields is invalid. (eg. invalid email or password ")

$this->form_validation->set_rules('email_address', 'Email Address', 'valid_email|required');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[4]');

How i can do that? Thanks for any help.

Edit: Sorry if my question is not clear. Currently I'm using validation_errors(), and i get errors of each field. However, I want to show a same error message if any of the two fields (email or password) is invalid. No matter if email is invalid, or if password is invalid, or both are invalid, it should print a single message, such as: invalid email or password.

4
how are you displaying the errors in the views ? - Vamsi Krishna B
@Krish at the moment i'm displaying all errors by <?php echo validation_errors(); ?>. But this method will print email and password both errors separately. I want to display only single error if any of the field is invalid. - Roman
a better question is, why do you want to display a single error? the user won't know which field they filled incorrectly - Mitchell McKenna
@Mitchell McKenna: I dont want to display many multiple errors such as, "email is invalid", "password is too small", "password is too large", "email does not exist in database", "password does not match" etc. and by the way almost all website's login system work same way (eg. yahoo, google ). And more important, I'm just curious to know. - Roman
@Wesley Murch. Im just wondering if there can be another way using form_error or set_message or validation_errors etc. - Roman

4 Answers

3
votes

I'm not sure if this is what you need, but you can try:

if($this->form_validation->run() == FALSE){
   $message = 'Your error message here'; //validation_errors() works too.
}else{
   $message = 'Your success message here';
}

$this->load->view('yourview',array('feedback_message'=>$message));

If you don't care which field isn't valid, then this snippet is ok. "Something is wrong, i don't care what's wrong, tell the user".

3
votes

Iterate over each field and check using form_error(), add any invalid field names to a single error string:

if($this->form_validation->run() == FALSE){
   $fields = array('email_address', 'password');
   $invalid_fields = array(); //where we'll store invalid field names
   foreach($fields as $field){
      if(form_error($field)){
         $invalid_fields[] = $field;
      }
   }
   $data['error_message'] = 'The following fields are invalid: ' . implode(",", $invalid_fields);
}
$this->load->view('yourview', $data); //if !empty($error_message) in view echo it out
1
votes

In your view you can just do this:

<?php if(!empty($this->form_validation->_error_array)): ?>
    <p>There were some errors.</p>
<?php endif; ?>
0
votes
$this->form_validation->set_message('rule', 'Error Message');

I think ,setting the same error message for both the rules will do the job ;)