Welcome Guest, Not a member yet? Register   Sign In
Validation error messages below or beside fields
#1

(This post was last modified: 04-21-2018, 06:55 PM by danangeloalcanar.)

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
Reply
#2

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
Reply
#3

Correct
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB