CodeIgniter Forums
Validation error messages below or beside fields - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Validation error messages below or beside fields (/thread-70525.html)



Validation error messages below or beside fields - danangeloalcanar - 04-21-2018

Hi CI community,

I have a simple question, but I can't produce the answer however I tried to think of it.

Here is my scenario:

I have a registration form. So users fill it out.

Now, if the validation criteria was not satisfied, the form will show validation errors right? 
Code:
<?php echo validation_errors(); ?>
The error messages will display above the form, right?

Now, here is what I want to accomplish. 

I want to show the error for each field, just below the field.

For example, I have a validation rule for the username, that it must be at least 6 characters long. If the user inputs 4 characters only,
I want the error to show just below the field. 

So if  I have error messages for username field and password field, the error messages for the username field will show below the username field, and the error messages for the password field will show below the password field. at the same time.

Now, here's a solution I am thinking but I don't know if it is the right way.

I will just set a flashdata for each field? and if it is set, I will just show it  like this:

Code:
if ($username < 6) {
    $this->session->set_flashdata('is_less_than_six', 'Username must be at least 6 characters long.');
}

And then create a span element below the username field:
Code:
if ($this->session->flashdata('is_less_than_six')) {
    echo $this->session->flashdata('is_less_than_six');
}

Is that a good solution? We know it will work, but how about repopulation the fields?

Hope you can guide me guys. I want to make my validation errors understandable to the users. Thankyou.

Cheers!

-Dan


RE: Validation error messages below or beside fields - danangeloalcanar - 04-21-2018

Hi CI Community,


I have already found the answer in the CI Documentation.

Showing Errors Individually




I learned that reading the documentation thoroughly must be the first thing to do! Blush


RE: Validation error messages below or beside fields - InsiteFX - 04-22-2018

Correct