duanhemou9834 2011-10-21 19:39
浏览 70
已采纳

使用最少的输入量在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.

  • 写回答

3条回答 默认 最新

  • dongqiao6730 2011-10-22 13:13
    关注

    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;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效