Form Generation

captainkuro edited this page Jan 10, 2013 · 16 revisions
Clone this wiki locally

Category:Libraries::Form Generation


Form Generation Class integrates the native form helper (with two added functions) and the validation library. It uses Code Igniter 1.5.x. IMPORTANT: To run this class in UNIX enviroment you must rename /system/application/config/form_generation.php to /system/application/config/Form_generation.php

Included files


/application/config/form_generation.php - Config file. Allowed attributes for each input type and autovalidation.
/application/libraries/Form_generation.php - The library itself.
/application/libraries/MY_Validation.php - Extension for validation library. Allows Javascript validation and Validation for linked dropdowns.
/helper/form_helper.php - Native helper with two extra methods.


/application/controllers/forms.php - Example controller
/application/forms/form_test.php - Example form sintaxis
/application/views/form_test.php - Example view

Class Methods

  • initialize: sets the config values
  • clear: reset all the added fields
  • add_input: recieve an associative array with the field data
  • get_input: recieve the input id and returns the output
  • get_label: recieve the input id and returns the label for that input
  • set_validation: sets the rules and field for the validation class. If the config autovalidation value is true, it will be called interanlly. Otherwise it must be called manually.


Inside a controller you have to add all inputs. the add_input method will receive an associative array with the field attributes. If no ID is setted, it will be copied from Name. The validation position will be passed to the valdiation class. The label will also be the field name for the validation fields.

    "type" => "text",
    "name" => "name",
    "label" => "Name",
    "maxlength" => "100",
    "validation" => "required"

    "type" => "password",
    "name" => "pass",
    "label" => "Password",
    "maxlength" => "100",
    "validation" => "required|min_length[4]"

Inside the view you have to use the open() method for the form and then print each field using get_input method.

<?php echo $this->form_generation->open(); ?>
<?php echo $this->form_generation->get_label("name"); ?>
<?php echo $this->form_generation->get_input("name"); ?>
<?php echo $this->form_generation->get_label("pass"); ?>
<?php echo $this->form_generation->get_input("pass"); ?>
<?php echo $this->form_generation->close(); ?>

Type: Select

For the select inputs you need to pass an associative array in the "OPTIONS" position.


    "type" => "select",
    "name" => "location",
    "label" => "Location",
    "value" => "Eu",
    "OPTIONS" => array(
        "Am" => "America",
        "Eu" => "Europe",
        "As" => "Asia",
        "Oc" => "Oceania",
        "Af" => "Africa"

Type: Linked select

For this inputs, it is necesary the OPTIONS position having a two dimensions array. The first level is the parent value and the second is the value for that position.


    "type" => "linked_select",
    "name" => "country",
    "label" => "Country",
    "parent" => "location",
    "OPTIONS" => array(
        "Am" => array(
            "US" => "United States",
            "AR" => "Argentina",
            "Ot" => "Other"
        "Eu" => array(
            "SP" => "Spain",
            "FR" => "France",
            "Ot" => "Other"
        "As" => array(
            "RU" => "Rusia",
            "IS" => "Israel",
            "Ot" => "Other"
        "Oc" => array(
            "AU" => "Australia",
            "NZ" => "New Zealand",
            "Ot" => "Other"
        "Af" => array(
            "CI" => "Cote d'Ivory",
            "EG" => "Egypt",
            "Ot" => "Other"

Type: Checkbox and Radio

For a checked radio/checkbox you can pass an array position called checked with true/false value .

    "type" => "checkbox",
    "name" => "accept",
    "value" => 1,
    "checked" => 1, //checked
    "label" => "Accept to licence agreement",
    "validation" => "isset"

    "type" => "radio",
    "name" => "gender",
    "id" => "gender_m",
    "checked" => 0, //not checked
    "value" => "m",
    "label" => "Male"

Loading posted values

The submitted values will overwrite the input value as default, except for the password field types.


I can't re-upload the class, and there are two small things to change. Line 379-381 (get_textarea method) should be overwritten with

function get_textarea($id) {
    $att = $this->_inputs[$id];
    if (isset($att['value'])) {
        $value = $att['value'];
    return form_textarea($att,$value);

Line 391-396 (get_linked_select)

function get_linked_select($id) {
    $data = array($this->_inputs[$id]['name'],$this->_inputs[$id]['parent'],$this->_inputs[$id]['options'],isset($this->_inputs[$this->_inputs[$id]['parent']]['value']) ? $this->_inputs[$this->_inputs[$id]['parent']]['value'] : "",isset($this->_inputs[$id]['value']) ? $this->_inputs[$id]['value'] : "");
    $parse = $this->_inputs[$id];
    return form_linked_dropdown($data[0],$data[1],$data[2],$data[3],$data[4],parse_form_attributes($parse,array()));