1

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.

5
  • 2
    how are you displaying the errors in the views ? Commented Jun 13, 2011 at 9:49
  • @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. Commented Jun 13, 2011 at 10:10
  • 2
    a better question is, why do you want to display a single error? the user won't know which field they filled incorrectly Commented Jun 13, 2011 at 10:11
  • 1
    @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. Commented Jun 13, 2011 at 10:19
  • 1
    @Wesley Murch. Im just wondering if there can be another way using form_error or set_message or validation_errors etc. Commented Jun 13, 2011 at 10:22

4 Answers 4

3

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".

Sign up to request clarification or add additional context in comments.

6 Comments

This is the logical solution if he only is using those two fields, or doesn't care to show errors for the others, but validation_errors() will show all the errors - not exactly what OP wants.
validation_errors() just outputs the errors? Doesn't it need an echo, print or whatever?
@Jorge: What I mean is that it contains a string with all the errors encountered validating the form. Exactly the opposite of what OP wants. Actually, his question is not very clear at all - he's asking to get messages for validating fields when the form validation messages actually apply to field/rule sets.
@Wesley Murch: Sorry if my question is not clear, I was just asking that by using validation_errors(), i get errors of each field. However, I want to show a same error message if any of the two fields (in my example) are 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.
@Roman: Just use Jorge's solution and don't use validation_errors() because that's not what it's for - it's for showing the actual validation errors. If the form did not validate - set the error message in whatever way you see fit - pass it to view, use flashdata, store it in an object or array... however you want. Any other solution that forces the use of that function will be a sloppy hack.
|
3

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

Comments

1

In your view you can just do this:

<?php if(!empty($this->form_validation->_error_array)): ?>
    <p>There were some errors.</p>
<?php endif; ?>

Comments

0
$this->form_validation->set_message('rule', 'Error Message');

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

3 Comments

Setting the same error message for both would not work, they both would be displayed if he's looping through all the errors.
To your edit - how would you actually use this in OP's example? It doesn't work because there are 2 fields and several rules.
mhm...even it may not be useful for the op.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.