How to perform form validations in Codeigniter

First you have to load the validation library in your controller’s constructor or in the method you are trying to perform validations. I recommend you to load it in the constructor so it will be available to all controller methods:

function __construct()
{
    parent::__construct();
    $this->load->library('form_validation');
}

Once the validation library is loaded, you can set the validation rules with below statement syntax:

$this->form_validation->set_rules('field_name','Human friendly name','rule 1|rule 2|rule N');

For an example, suppose you have the following form with two input fields in it:

<form>
   <input type="text" name="name" value=""/>
   <input type="text" name="email" value=""/>
</form>

Now if you want to validate those two form fields (name and email) you would set two validation rules as shown in the below example:

$this->form_validation->set_rules('name','Name','required');
$this->form_validation->set_rules('email','E-Mail Address','required|valid_email');

Notice how we’ve used HTML name attribute value of each input field as the field name in respective validation rules.

After setting validation rules, you can actually perform the validations by running below statement:

$this->form_validation->run()

If the validations are successful, the above statement will return TRUE. If a validation rule fails, it will return FALSE. So in practice you’d want to put above statement within an if-else conditional block as shown below:

if($this->form_validation->run())
{
    // validation passed
}
else
{
    // validation failed
}

If the validation passes you may process the data by storing in a database or emailing somewhere, …etc. However, if the validation fails you’d want to show the form again with error messages explaining what went wrong. You can call validation_errors() function from your view templates to retrieve all validation errors.

Below example shows how to utilize validation_errors() function in your view templates:

<?php echo validation_errors(); ?>

<form>
   <input type="text" name="name" value=""/>
   <input type="text" name="name" value=""/>
</form>

Instead of showing all validation errors as a list, you can also show errors for each individual field (if any) by using form_error(‘field name’) function as shown in the below example:

<form>
    <?php form_error('name'); ?>
    <input type="text" name="name" value=""/>

    <?php form_error('email'); ?>
    <input type="text" name="email" value=""/>
</form>

Note that you have to replace field name with the real name of your input field.

Additionally, you can also pre-fill each field with the values user has submitted last time by using set_value(‘field name’) function:

<form>
     <?php form_error('name'); ?>
     <input type="text" name="name" value="<?php echo set_value('name'); ?>"/>

     <?php form_error('email'); ?>
     <input type="text" name="email" value="<?php echo set_value('email'); ?>"/>
</form>

In the above code, see how we have put set_value() function as the value attribute for each input field.

Setting custom validation error messages (pending):

Comments

So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sidebar



Do NOT follow this link or you will be banned from the site!