Code Igniter and dynamic forms

How many times did you stumble on occasions where you need to generate a form, but you want to maintain the dynamic characteristics of CodeIgniter? For example imagine extensible forms which are shaped by the users preferences, a user registration for example where the administrator can choose to add more fields for the registration, or remove some of them.
My initial approach on this situation in CodeIgniter was to create a helper class on my application which has a generateForm method, the method get injected by data that the controller find in configuration files, and thus a new field is easily populated without the need for tedious replacements inside controllers.
Here is a quick example:
First we have the config file:
if (!defined('BASEPATH')) exit('No direct script access allowed');
$config['fields']['settings']['base_url'] = array(
    'required' => true,
    'type' => 'input',
    'db_field' => 'base_url',
    'label' => 'Base Url',
    'form_attributes' => array(
        'name' => 'base_url',
        'id' => 'form_base_url',
        'size' => '50',

The my controller:
function index()
    $this->config->load('config_fields', TRUE);
    $tmpfields = $this->config->item('fields', 'config_fields');
    $data['fields'] = $tmpfields['settings'];
    $data['title'] = 'System Settings';
    $data['attributes'] = 'id="settings_form"';
    $form['system_settings_form'] = $this->htmlhelper->generateForm($data);
    $out = $this->load->view('sub/system_settings', $form, TRUE);

And my htmlhelper library has the following method:
function generateForm($data = array())
    $out = null;
    $title = array_key_exists('title', $data) ? $data['title'] : null;
    $fields = array_key_exists('fields', $data) ? $data['fields'] : array();
    $attributes = array_key_exists('attributes', $data) ? $data['attributes'] : null;
    $method = array_key_exists('method', $data) ? $data['method'] : null;
    $action = array_key_exists('action', $data) ? $data['action'] : null;
    if( !empty($fields) )
        $out = "\t\t<table $attributes>\n";
        $out .= "\t\t\t<tr>\n";
        $out .= "\t\t\t\t<caption>$title</caption>\n";
        $out .= "\t\t\t</tr>\n";
        foreach ($fields as $field)
            $form_attributes = $field['form_attributes'];
            $type = array_key_exists('type', $field) ? $field['type'] : 'input';
            $label = array_key_exists('label', $field) ? $field['label'] : null;
            $out .= "\t\t\t<tr>\n";
            $out .= "\t\t\t\t<td id=\"col1\">$label</td><td>";
            $out .= call_user_func('form_'.$type, $form_attributes);
            $out .= "</td>\n\t\t\t</tr>\n";
        $out .= "</table>";
    return $out;

Off course this is a very primer stage of the method, it could have more items added to it (additionaly with checks) the only need from the user side when changing fields is to add them inside the config file and off course modify the DB table to hold the new data.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.