Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by {author} )

Revision: Form Generation

Revision from: 14:01, 11 Feb 2007

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

Included files

Core

/application/config/form_generator.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.

download: File:form generation.zip