I wrote a set of code which currently lives in MY_Controller which uses the $config that you write for form validation to generate a form.
Along the way it creates the form, adds * to the labels etc
I'll dig it out and post it on Monday.
I'd prefer it to be a library but my knowledge of CI is still growing...
ADDED MY MY_controller.php details:
I have the following in my MY_Controller.php file
function generate_form($config, $legend = 'Details', $wraptag = 'div', $buttontext = 'update')
{
$fields = array();
$files = 0;
if($config)
{
foreach($config as $c)
{
if(strpos($c['rules'],'required') === FALSE) {
$label = $c['label'];
}
else
{
$label = $c['label'].' <span class="required">*</span>';
}
$data = array(
'name' => $c['field'],
'id' => $c['field'],
'value' => set_value($c['field'], $c['value']),
'class' => $c['field'],
);
$fields[] = '<'.$wraptag.'>'."
";
$func = 'form_'.$c['input_type'];
switch ($c['input_type'])
{
case 'displayonly':
$fields[] = form_label($label, $c['field'])."
";
$fields[] = '<input type="text" value="'.$c['value'].'" disabled="disabled"/>';
break;
case 'dropdown':
case 'multiselect':
$fields[] = form_label($label, $c['field'])."
";
$fields[] = $func($c['field'], $c['options'], $c['value']);
break;
case 'datepicker':
$fields[] = form_label($label, $c['field'])."
";
$fields[] = $func($c['field'])."
";
break;
case 'timezone':
$fields[] = form_label($label, $c['field'])."
";
$fields[] = timezone_menu($c['value'],$c['field'], $c['field']);
break;
case 'upload':
$fields[] = form_label($label, $c['field'])."
";
$fields[] = $func($data)."
";
$files = 1;
break;
default:
$fields[] = form_label($label, $c['field'])."
";
$fields[] = $func($data)."
";
break;
}
$fields[] = '</'.$wraptag.'>'."
";
}
$fields[] = '<'.$wraptag.'>'."
";
$fields[] = form_submit('btnSubmit', 'Update');
$fields[] = '</'.$wraptag.'>'."
";
$form_start[] = validation_errors('<div class="error">', '</div><!--class="error"-->');
if($files)
{
$form_start[] = form_open_multipart(uri_string());
}
else
{
$form_start[] = form_open(uri_string());
}
$form_start[] = '<fieldset>';
$form_start[] = '<legend>'.$legend.'</legend>';
$form_end[] = '</fieldset>'."
";
$form_end[] = form_close();
}
if($wraptag == 'li')
{
$fields_start = '<ul>';
$fields_end = '</ul>';
}
else
{
$fields_start = '';
$fields_end = '';
}
return (implode('',$form_start).$fields_start.implode('',$fields).$fields_end.implode('',$form_end));
}
function create_validation_from_config($config)
{
foreach($config as $c)
{
if($c['rules'] != '')
{
if($c['input_type'] == 'datepicker')
{
$validation[] = array(
'field' => $c['field'].'_day',
'label' => $c['label'],
'rules' => $c['rules'],
);
$validation[] = array(
'field' => $c['field'].'_month',
'label' => $c['label'],
'rules' => $c['rules'],
);
$validation[] = array(
'field' => $c['field'].'_year',
'label' => $c['label'],
'rules' => $c['rules'],
);
}
else
{
$validation[] = array(
'field' => $c['field'],
'label' => $c['label'],
'rules' => $c['rules'],
);
}
}
}
return $validation;
}