If I used this
<?= validation_errors(); ?>
the result is
Username field is required
Password field is required
<textbox for username>
<textbox for password>
what I actually need is to display those errors inline with their element
example:
<textbox for username> Username field is required
<textbox for password> Password field is required
Validations errors are errors when users do not respond to mandatory questions. A validation error occurs when you have validation/response checking turned on for one of the questions and the respondent fails to answer the question correctly (for numeric formatting , required response).
To set validation rules you will use the set_rules() method: $this->form_validation->set_rules(); The above method takes three parameters as input: The field name - the exact name you've given the form field. A “human” name for this field, which will be inserted into the error message.
Validation Rule Reference. Given below are the most commonly used list of native rules available to use. Returns FALSE if the form element is empty. Returns FALSE if the form element does not match the one in the parameter.
This is covered in the user guide:
http://codeigniter.com/user_guide/libraries/form_validation.html#individualerrors
Showing Errors Individually
If you prefer to show an error message next to each form field, rather than as a list, you can use the
form_error()
function.
So if your field name is username
, you would use form_error('username')
.
Important Note: If you use an array as the name of a form field, you must supply it as an array to the function. Example:
<?php echo form_error('options[size]'); ?>
This is a shortcut for $this->form_validation->error()
, which you can also use if you desire.
In order to display error individually you should use the function form_error('username'). And for you to get a value of a field being checked, use the function set_value('username').
For these two functions to work, you would have had to, in your controller, set a rule for the 'username' field. Where you specify wich validation rules apply to that field.
<?php echo form_error('username'); ?>
<input type="text" name="username" value="<?php echo set_value('username'); ?>">
Here is a simple tutorial about form Login
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With