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 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器