I am building an application full of forms and so I thought it is good idea to create a class to populate form fields.
Here is my class and I am sure it can be optimized more than now and could be better.
class JS_Forms
{
public function __construct()
{
//$this->register_fields($fields);
}
public function register_fields($fields)
{
$text_array = array_flip(array('type', 'name', 'id', 'placeholder', 'required', 'data-length', 'class', 'value'));
$uploads = array_flip(array('type', 'name'));
foreach ($fields as $value)
{
switch ($value['type'])
{
case 'text':
echo '<div class="form-group">';
echo form_label(lang($value['label']), $value['id'], array('class' => $value['label_class']));
unset($value['label_class']);
unset($value['label']);
$value['value'] = set_value($value['id'], is_null($value['obj']) ? $value['std'] : $value['obj']);
echo array_key_exists('field_class', $value) ? '<div class="' . $value['field_class'] . '">' : NULL;
echo form_input(array_intersect_key($value, $text_array));
echo array_key_exists('field_class', $value) ? '</div>' : NULL;
echo '</div>';
break;
case 'upload':
echo '<div class="form-group">';
echo form_label(lang($value['label']), $value['name'], array('class' => $value['label_class']));
unset($value['label_class']);
unset($value['label']);
unset($value['value']);
//$value['value'] = set_value($value['id'], is_null($value['obj']) ? $value['std'] : $value['obj']);
echo array_key_exists('field_class', $value) ? '<div class="' . $value['field_class'] . '">' : NULL;
echo form_upload(array_intersect_key($value, $uploads));
echo array_key_exists('field_class', $value) ? '</div>' : NULL;
echo '</div>';
break;
case 'jupload':
echo '<div class="form-group">';
echo form_label(lang($value['label']), $value['name'], array('class' => $value['label_class']));
unset($value['label_class']);
unset($value['label']);
unset($value['value']);
echo array_key_exists('field_class', $value) ? '<div class="' . $value['field_class'] . '">' : NULL;
echo '<div class="fileinput fileinput-new" data-provides="fileinput">';
echo '<div class="input-group">';
echo '<div class="form-control uneditable-input" data-trigger="fileinput">';
echo '<i class="fa fa-file fileinput-exists"></i> <span class="fileinput-filename"></span>';
echo '</div>';
echo '<span class="input-group-addon btn btn-default btn-file">';
echo '<span class="fileinput-new">Select file</span>';
echo '<span class="fileinput-exists">Change</span>';
echo form_upload(array_intersect_key($value, $uploads));
//
echo '</span>';
echo '<a href="#" class="input-group-addon btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a>';
echo '</div>';
echo '</div>';
echo array_key_exists('field_class', $value) ? '</div>' : NULL;
echo '</div>';
break;
case 'textarea':
echo '<div class="form-group">';
echo form_label(lang($value['label']), $value['id'], array('class' => $value['label_class']));
unset($value['label_class']);
unset($value['label']);
$value['value'] = set_value($value['id'], is_null($value['obj']) ? $value['std'] : $value['obj']);
echo array_key_exists('field_class', $value) ? '<div class="' . $value['field_class'] . '">' : NULL;
echo form_textarea($value);
echo array_key_exists('field_class', $value) ? '</div>' : NULL;
echo '</div>';
break;
case 'select':
echo '<div class="form-group">';
echo form_label(lang($value['label']), $value['id'], array('class' => $value['label_class']));
unset($value['label_class']);
unset($value['label']);
echo array_key_exists('field_class', $value) ? '<div class="' . $value['field_class'] . '">' : NULL;
// Important! here you need to set selected item value using core helper 'set_selected()'
echo form_dropdown($value['name'], $value['options'], set_value($value['id'], is_null($value['obj']) ? $value['std'] : $value['obj']), 'class="' . $value['class'] . '"');
echo array_key_exists('field_class', $value) ? '</div>' : NULL;
echo '</div>';
break;
case 'multiselect':
echo '<div class="form-group">';
echo form_label(lang($value['label']), $value['id'], array('class' => $value['label_class']));
unset($value['label_class']);
unset($value['label']);
echo array_key_exists('field_class', $value) ? '<div class="' . $value['field_class'] . '">' : NULL;
//---
$id = array_key_exists('id', $value) ? 'id="' . $value['id'] . '"' : NULL;
$class = array_key_exists('class', $value) ? 'class="' . $value['class'] . '"' : 'class="multi-select"';
$name = array_key_exists('name', $value) ? $value['name'] : NULL;
echo '<select multiple="multiple" ' . $class . $id . ' name="' . $name . '[]">';
$items = $value['options'];
$selects = explode(',', $value['obj']);
echo '<option value="null" selected class="disabled" style="display:none">Selected</option>';
foreach ($items as $key => $item)
{
$selected = in_array($key, $selects) ? 'selected="selected"' : NULL;
echo '<option value="' . $key . '" ' . $selected . '>' . $item . '</option>';
}
echo '</select>';
echo array_key_exists('field_class', $value) ? '</div>' : NULL;
echo '</div>';
break;
case 'radio':
echo '<div class="form-group">';
echo form_label(lang($value['label']), $value['name'], array('class' => $value['label_class'], 'style' => $value['style']));
$options = $value['options'];
unset($value['label_class']);
unset($value['label']);
echo array_key_exists('field_class', $value) ? '<div class="' . $value['field_class'] . '">' : NULL;
foreach ($value['options'] as $key => $radio)
{
echo (array_key_exists('block', $value) && $value['block'] == TRUE) ? '<div class="radio">' : NULL;
echo form_label(form_radio(array('name' => $value['name'], 'id' => $value['name'] . '-' . $key), $key, $value['std'] == $key ? TRUE : FALSE, ($key == $value['obj']) ? set_radio($value['name'], $value['obj'], TRUE) : set_radio($value['name'], $key)) . $radio, $value['name'] . '-' . $key, ((array_key_exists('block', $value) && $value['block'] == TRUE) ? NULL : array('class' => $value['radio_lable_class'])));
echo (array_key_exists('block', $value) && $value['block'] == TRUE) ? '</div>' : NULL;
}
echo array_key_exists('field_class', $value) ? '</div>' : NULL;
echo '</div>';
break;
case 'checkbox':
echo '<div class="form-group">';
echo form_label(lang($value['label']), $value['name'], array('class' => $value['label_class'], 'style' => $value['style']));
$options = $value['options'];
unset($value['label_class']);
unset($value['label']);
echo array_key_exists('field_class', $value) ? '<div class="' . $value['field_class'] . '">' : NULL;
echo (array_key_exists('required', $value) && $value['required'] == TRUE) ? form_hidden($value['name'] . '[]', 'null') : NULL;
foreach ($value['options'] as $key => $checkbox)
{
$checked = explode(',', $value['obj']);
echo (array_key_exists('block', $value) && $value['block'] == TRUE) ? '<div class="checkbox">' : NULL;
echo form_label(form_checkbox(array('name' => $value['name'] . '[]', 'id' => $value['name'] . '-' . $key), $key, in_array($key, $checked) ? set_checkbox($value['name'], $key, TRUE) : FALSE) . $checkbox, $value['name'] . '-' . $key, ((array_key_exists('block', $value) && $value['block'] == TRUE) ? NULL : array('class' => $value['checkbox_lable_class'])));
echo (array_key_exists('block', $value) && $value['block'] == TRUE) ? '</div>' : NULL;
}
echo array_key_exists('field_class', $value) ? '</div>' : NULL;
echo '</div>';
break;
default:
break;
}
}
}
}
Usage
$fields = array(
array(
'type' => 'text',
'label' => 'emp_employee_id',
'label_class' => 'col-sm-3 control-label',
'field_class' => 'col-sm-9',
'name' => 'employee_id',
'id' => 'employee_id',
'placeholder' => 'e.g. 912',
'required' => 'required',
'data-minlength' => 3,
'class' => 'form-control',
'std' => '',
'obj' => segment_is_num(4, $employee, 'first_name', NULL),
),
array(
'type' => 'text',
'label' => 'emp_firstname',
'label_class' => 'col-sm-3 control-label',
'field_class' => 'col-sm-9',
'name' => 'first_name',
'id' => 'first_name',
'placeholder' => 'The Place Holder',
'required' => 'required',
'data-minlength' => 3,
'class' => 'form-control',
'std' => 'Jatin',
'obj' => segment_is_num(4, $employee, 'first_name', NULL),
),
array(
'type' => 'select',
'label' => 'emp_lastname',
'label_class' => 'col-sm-3 control-label',
'field_class' => 'col-sm-9',
'name' => 'last_name',
'id' => 'last_name',
'class' => 'form-control',
'options' => array('one' => 'One', 'two' => 'Two', 'three' => 'Three'),
'std' => 'three',
'obj' => segment_is_num(4, $employee, 'last_name', NULL),
),
array(
'type' => 'radio',
'label' => 'emp_gender',
'label_class' => 'col-sm-3 control-label',
'field_class' => 'col-sm-9',
'radio_lable_class' => 'radio-inline',
'name' => 'gender',
'options' => array('male' => 'Male', 'female' => 'Female'),
'std' => 'male',
'style' => 'display: block',
'obj' => segment_is_num(4, $employee, 'gender', NULL),
),
array(
'type' => 'checkbox',
'block' => false,
'label' => 'emp_country',
'label_class' => 'col-sm-3 control-label',
'field_class' => 'col-sm-9',
'checkbox_lable_class' => 'checkbox-inline',
'name' => 'country',
'options' => array('india' => 'India', 'mauritius' => 'Mauritius', 'japan' => 'Japan'),
'std' => 'india',
'style' => 'display: block',
'required' => TRUE,
'obj' => segment_is_num(4, $employee, 'country', NULL),
),
array(
'type' => 'textarea',
'label' => 'emp_attrition_comment',
'label_class' => 'col-sm-3 control-label',
'field_class' => 'col-sm-9',
'name' => 'attrition_comment',
'id' => 'attrition_comment',
'placeholder' => 'The Place Holder',
'class' => 'form-control',
'std' => 'Soni',
'obj' => segment_is_num(4, $employee, 'attrition_comment', NULL),
),
array(
'type' => 'multiselect',
'label' => 'emp_nationality',
'label_class' => 'col-sm-3 control-label',
'field_class' => 'col-sm-9',
'name' => 'nationality',
'id' => 'nationality',
'class' => 'form-control',
'options' => array('indian' => 'Indian', 'mauritian' => 'Mauritian', 'american' => 'American'),
'std' => 'indian',
'obj' => segment_is_num(4, $employee, 'nationality', NULL),
),
array(
'type' => 'jupload',
'label' => 'emp_nid',
'label_class' => 'col-sm-3 control-label',
'field_class' => 'col-sm-9',
'name' => 'employee_snap',
),
);
// populate form firles using the 'js_forms' library
$this->js_forms->register_fields($fields);
Usage
code. I am using array to populate the form. But may be in future I can store such thing into database and populate using rows as well. \$\endgroup\$__construct()
as well \$\endgroup\$