Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 16 revisions

Category:Libraries::Form Generation

[h3]Introduction[/h3]

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

[h3]Included files[/h3]

[b]Core[/b]

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

[b]Example[/b]

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

[h3]Class Methods[/h3]

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

[h3]Usage[/h3]

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.

[code] $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]" )); [/code]

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

[code] <?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(); ?> [/code]

[h4]Type: Select[/h4]

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

Example [code] $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" ) )); [/code]

[h4]Type: Linked select[/h4]

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 [code] $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" ) ) )); [/code]

[h4]Type: Checkbox and Radio[/h4]

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

[code] $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" )); [/code]

[h4]Loading posted values[/h4] The submitted values will overwrite the input value as default, except for the password field types.

[h3]Fixes[/h3] 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 [code] 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); } [/code]

Line 391-396 (get_linked_select) [code] 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())); } [/code]

download: File:form_generation.zip

Note from a user: don't forget to set an action for your form: [code] <?php $this->form_generation->_form_attributes['action'] = 'controller/method'; ?> [/code]

Clone this wiki locally