Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter: How can I display single validation error?

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
like image 545
user1033600 Avatar asked May 19 '12 02:05

user1033600


People also ask

What is err validation?

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

What Is syntax to set rule of validation in CodeIgniter?

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.

Which rules returns false if the form element is empty?

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.


2 Answers

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.

like image 26
Wesley Murch Avatar answered Sep 30 '22 08:09

Wesley Murch


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

like image 192
Moyed Ansari Avatar answered Sep 30 '22 07:09

Moyed Ansari