Codeigniter Show Individual Custom Error

I am trying to verify my registration form and I have rules in my Username and Password fields.

$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean'); $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database'); 

So, my callback_check_database function is in the Password field.

So, in my function for check_database, I have:

 $this->form_validation->set_message('check_database', 'Invalid username or password'); return false; 

So, I am setting up a custom message that I want to display.

In my opinion, I want to individually show this check_database error. I do not want to show any other errors.

Here is what I tried:

 // does not work <?php echo form_error('check_database'); ?> // this works <?php echo form_error('password'); ?> 

since it is intended for the Password field, it will only be displayed when I repeat the password errors. But this means that since the password is a required field, if I do not find anything in it, an error will be displayed indicating that the Password field is required.

Is there a way to display only my custom error message?

+4
source share
1 answer

You can solve this in two ways. Both have advantages and disadvantages.

Option 1:

 $this->form_validation->set_rules('password', 'Password', 'callback_check_database'); 

Then, inside callback_check_database, just enable validation to make sure the value is $ value! = "(Which replicates the" required "field).

Note. In any case, you should not use xss_clean in the password field (since it will change the password - google, and you will see examples), and when you receive the password, you will get a zero advantage.

Option 2:

Inside the view file just enter

 <?php if (form_error('password')) { echo "Invalid username or password"; } ?> 

This means that you are not actually repeating the error, you are just checking to see if there is an error for the password field, and no matter what it is, you only show the text above.

+3
source

All Articles