Tuesday, December 4, 2012

Cleaner Form Validation Rules in CODEIGNITER

Introduction

I have been using Codeigniter for some time now , i and i have been impressed by how fast and easy it is to develop websites and web applications in codeigniter. In creating websites usually the standard thing to do , is to validate any user inputs. in codeigniter's userguide, which is the best userguide documentation so far, they have a built in form validation class, that allows developers to use validation to the highest level.


Today i want to share how we can clean up our controllers or whatever you do , and put our form validation rules , in one place so that we can have a nicer and cleaner code.

For most of you. well not all of you, tried to put the form validation on your controller like this.

class login Extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
}
public function index()
{
$this->form_validation->set_rules('field','field name','rules');
$this->form_validation->set_rules('field','field name','rules');
$this->form_validation->set_rules('field','field name','rules');
$this->form_validation->set_rules('field','field name','rules');
$this->form_validation->set_rules('field','field name','rules');
$this->form_validation->set_rules('field','field name','rules');
$this->form_validation->set_rules('field','field name','rules');
$this->form_validation->set_rules('field','field name','rules');
$this->form_validation->set_rules('field','field name','rules');
$this->form_validation->set_rules('field','field name','rules');

if($this->form_validation-run() == FALSE)
{
// do this
}else
{
// do that
}
}
}

Well this is not a bad practice for some, but what we are trying to achieve here is the cleanliness and maintainability of our code. What if your in a registration form where a hundred or more rules need to be set?, would you like to clutter your controllers and render them to be unreadable to the next person?.

The solution is to create another file that holds all rules for the form validation class, how?

STEP1. Initialize Directory


On your Application/Config Directory create a file and name it form_validation , exactly how i type it.
when loading the form_validation class in a controller it checks if form_validation directory exists in you Application/Config Directory if none then will proceed to the manual $this->form_validation->set_rules() existing on your controller.


STEP 2. Define The Rules


The structure of the form_validation must be a multidimensional array , note that the variable holding all the array must be named config. look at the example bellow , you will see a key named LOGIN and FORGOT_PASS, the array need three keys the field,label and rules.
    -> the field is the name of your input field, 
    -> the label label of the input field is a string that will be displayed on the form_validation() or form_error() if it has no value the field will be used in its place.
    -> and the rules are the rules defined in the rule reference.


<?php                                                                                    
$config = array(                                                                       
       'login' => array(                                       
                  array(                                                                     
                    'field' => 'username',
                    'label' => 'username',
                    'rules' => 'required'
                  ),
                  array(
                    'field' => 'password'
                    'label' => 'password'
                    'rules' => 'required'
        ),
        'forgot_pass' = array(
                  'field' => 'email',
                  'label' => 'email address',
                  'rules'  => 'required|valid_email'
        )
);
?>

STEP 3. USE IT!!!!

Now we have defined the direactory and the rules, it time we use it. HOW?
as i have told you earlier when loading the form_validation library, it will detect if the form_validation file exists in Application/Config directory.

on your controller, just put the key of the rule inside
$this->form_validation-run('put_the_key_here')


i.e


class login Extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
}
public function index()
{
if($this->form_validation-run('login') == FALSE)
{
// do this
}else
{
// do that
}
}
}


if you followed it all then no problem will be achieved, and hey the form validation user guide  also has a topic about this one, go and check it out.

No comments:

Post a Comment