Skip to content
captainkuro edited this page Jan 10, 2013 · 16 revisions

Category:Libraries::Form Generation

Introduction

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

Core

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

Example

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

Usage

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.

$this->load->library('form_generation');
$this->form_generation->add_input(array(
    "type" => "text",
    "name" => "name",
    "label" => "Name",
    "maxlength" => "100",
    "validation" => "required"
));

$this->form_generation->add_input(array(
    "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.

Example

$this->form_generation->add_input(array(
    "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.

Example

$this->form_generation->add_input(array(
    "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 .

$this->form_generation->add_input(array(
    "type" => "checkbox",
    "name" => "accept",
    "value" => 1,
    "checked" => 1, //checked
    "label" => "Accept to licence agreement",
    "validation" => "isset"
));

$this->form_generation->add_input(array(
    "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.

Fixes

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'];
        unset($att['value']);
    }
    unset($att['type']);
    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];
    unset($parse['name'],$parse['parent'],$parse['options'],$parse['parent'],$parse['value'],$parse['type']);
    return form_linked_dropdown($data[0],$data[1],$data[2],$data[3],$data[4],parse_form_attributes($parse,array()));
}

download: File:form_generation.zip

Category:Contributions::Libraries::HTML

Clone this wiki locally