CodeIgniter - Extending Form Validation

Overview
1. Store all form validation in application/config/form_validation.php
2. Create a library to extend form validation for callback functions
3. Run the form validation inside the controller method

Step 1: Create form_validation.php
Create the file application/config/form_validation.php then add your form validations

In my example, I’ll do something like the code below which validates the inputs when a user creates a news article

$config = array(
    'article' => array(
        array(
            'field' => 'news_title',
            'label' => 'Title',
            'rules' => 'trim|required|max_length[100]'
        ),
        array(
            'field' => 'news_body',
            'label' => 'Body',
            'rules' => 'trim|required'
        ),
        array(
            'field' => 'news_category',
            'label' => 'Category',
            'rules' => 'trim|required|validate_category'
        ),
    ),
);

Now, all of the rules in my form validation is in CI Rule Reference except for the validate_category which is my own validation function. What it does is it checks if the provided category actually exists in the database.

According to CI documentation, you have to add callback_ before the method name.but that’s only if you add your custom validation function inside the same controller where you are running the form validation.

However the validation functions in this tutorial will be put in one separate file in case you want to use the same function in other controllers

Step 2: Create MY_Form_validation.php
Create the file MY_Form_validation.php in application/libraries to add your callback functions

class MY_Form_validation extends CI_Form_validation 
{
    public function __construct($config = array())
    {
        parent::__construct($config);
    }

    /**
     * Get the CI instance
     */
    public function __get($object)
    {
        $this->CI = &get_instance();
        return $this->CI->$object;
    }

    /**
     * Checks the table DB if the specified category exists
     */
    public function validate_category($id)
    {
        $category = $this->CI->db
                        ->limit(1)
                        ->get_where('news_category', array('id' => $id))
                        ->row();

        if($category) {
            return TRUE;
        }

        $this->CI->form_validation
                 ->set_message('validate_category','Invalid category');

        return FALSE;
    }
}

Step 3: Run the form validation inside the controller

class News extends CI_Controller 
{
    public function add()
    {
        if ($this->form_validation->run('article') === FALSE)
        {
            // code when form validation fails
        }
    
        // code when form validation succeed
    }
}

Now you can add all your custom validations in MY_Form_validation.php
They can now be re-used in other controllers and it is easy to modify them in case something went wrong because they are just in one place.

Comments

Post a Comment