使用最少的输入量在CodeIgniter中创建新/编辑表单

It is incredibly common to create a "New" or "Edit" form in CodeIgniter (or any framework for that matter).

Without resorting to automated scaffolding, what is the quickest / easiest way to create these forms in CodeIgniter with the least amount of typing/fuss/etc.

The ideal solution should handle many elements, validate itself before submitting, should not clear the form if a mistake is made, and still be readable by the developer. It would not rely on a "generator" script.

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;

}

NETTE framework has an automated auto-validate system which give feedback to user through javascript (client-side) and php (server-side) - Link.

Maybe you could write something similar in CI (with understanding Nette source files) or you can use Nette framework instead..

I'm using CI with Smarty. I've created an add.tpl smarty-template, and a modify.tpl which extends add.tpl. My controller has modify and add function which loads modify.tpl and add.tpl. All tpls display a table: thead and tbody. The modify.tpl displays one more line in thead: the original datas. And modify.tpl displays/sets the original values in their input fields.

I've a simple data structure which describes what datas should get from users, and the tpl-files know, how should displays their input-fields (e.g. inputbox, select, checkbox, etc.).

If you don't want use plus framework (Smarty), I think it isn't too hard to create similar thing in PHP/CI.