laravel 表单封装改进

namespace App\Form;

class BaseFormGroup {

    public function form_group_box($label, $icon, $field, $helper, $required) {
        //组合顺序, 媒体, label, 表单域,提示
        $label_html = $this->form_label_box($label, $required);
        $input_html = $this->form_input_box($field, $icon);
        $helper_html = $this->form_helper_box($helper);
        $content = $label_html . $input_html . $helper_html;
        return '<div class="form-group">' . $content . '</div>';
    }

    public function media_form_group_box($medias, $label, $icon, $field, $helper, $required) {
        //组合顺序, 媒体, label, 表单域,提示
        $medias_html = $this->form_media_box($medias);
        $label_html = $this->form_label_box($label, $required);
        $input_html = $this->form_input_box($field, $icon);
        $helper_html = $this->form_helper_box($helper);
        $content = $medias_html . $label_html . $input_html . $helper_html;
        return '<div class="form-group">' . $content . '</div>';
    }
    
    /**
     * 输入型表单
     */
    public function text_input_field($name, $module, $label, $value) {
        $mudule_data = $this->get_input_module($name, $module);
        $field = '<input type="text" id="'. $mudule_data['id'] .'" name="'. $mudule_data['name'] .'" value="'. $value .'" class="form-control" placeholder="请输入'. $label .'" />';
        return $field;
    }

    /**
     * textarea表单
     */
    public function textarea_input_field($name, $module, $label, $value) {
        $mudule_data = $this->get_input_module($name, $module);
        $field = '<textarea id="'. $mudule_data['id'] .'" name="'. $mudule_data['name'] .'" class="form-control" placeholder="请输入'. $label .'">'. $value .'</textarea>';
        return $field;
    }

    public function editor_input_field($name, $module, $label, $value) {
        $mudule_data = $this->get_input_module($name, $module);
        $field = '<div id="content">'.$value.'</div>';
        $field .= '<input type="hidden" id="'. $mudule_data['id'] .'" name="'. $mudule_data['name'] .'" value="'. htmlentities($value) .'" class="wangeditor_value" />';
        return $field;
    }

    /**
     * 媒体型表单
     */
    public function number_input_field($name, $module, $label, $value) {
        $mudule_data = $this->get_input_module($name, $module);
        $field = '<input type="text" id="'. $mudule_data['id'] .'" name="'. $mudule_data['name'] .'" value="'. $value .'" class="form-control" placeholder="请输入'. $label .'" style="width:200px" />';
        return $field;
    }

    /**
     * 媒体型表单
     */
    public function file_input_field($name, $module, $label, $value) {
        $mudule_data = $this->get_input_module($name, $module);
        $field = '<input type="file" id="'. $mudule_data['id'] .'" name="'. $mudule_data['name'] .'" value="'. $value .'" class="form-control" placeholder="请上传'. $label .'" />';
        return $field;
    }

    /**
     * 媒体型表单
     */
    public function multiple_file_input_field($name, $module, $label, $value) {
        $mudule_data = $this->get_input_module($name, $module);
        $field = '<input type="file" id="'. $mudule_data['id'] .'" name="'. $mudule_data['name'] .'" value="'. $value .'" multiple class="form-control" placeholder="请上传'. $label .'" />';
        return $field;
    }

     /**
     * 下拉表单
     */
    public function select_field($name, $module, $value, $data) {
        $mudule_data = $this->get_input_module($name, $module);
        $options = '';
        foreach($data as $key => $val) {
            if ($key == $value) {
                $options .= '<option value="'. $key .'" selected="selected">'. $val .'</option>';
            } else {
                $options .= '<option value="'. $key .'">'. $val .'</option>';
            }
        }
        $field = '<select id="'. $mudule_data['id'] .'" name="' . $mudule_data['name'] . '" class="form-control">'. $options .'</select>';
        return $field;
    }

    /**
     * radio表单
     */
    public function radio_field($name, $module, $value, $data) {
        $mudule_data = $this->get_input_module($name, $module);
        $field = '<div>';
        foreach($data as $key => $val) {
            $selected = '';
            if ($key == $value) {
                $selected = 'selected="selected"';
            }
            $field .= '<label class="radio-inline"><input type="radio" id="'. $mudule_data['id'] . '_' . $key .'" name="'. $mudule_data['name'] .'" value="'. $key .'" '. $selected .'>'. $val . '</label>';
        }
        $field .= '</div>';
        return $field;
    }

    /**
     * 表单label
     */
    private function form_label_box($label, $required = false) {
        if ($required) {
            $label = $label . ' <span>*</span>';
        }
        return '<label class="col-sm-2  control-label">'. $label .'</label>';
    }

    /**
     *  表单域
     */
    private function form_input_box($field, $icon = '') {
        $form_icon = '';
        if(!empty($icon)) {
            $form_icon = '<span class="input-group-addon"><i class="fa fa-'. $icon .' fa-fw"></i></span>';
        }
        return '<div class="col-sm-8"><div class="input-group">'.$form_icon . $field . '</div></div>';
    }

    /**
     * 表单提示内容
     */
    private function form_helper_box($helper) {
        $form_helper = '';
        if (!empty($helper)) {
            $form_helper = '<p class="help-block">'. $helper .'</p>';
        }
        return $form_helper;
    }

    /**
     * 表单媒体文件,如图片,视频
     */
    private function form_media_box($medias) {
        $html = '';
        if (!empty($medias)) {
            $html = '<div class="row media-list"><div class="col-sm-2">&nbsp;</div><div class="col-sm-8">';
            $html .= '<ul class="clearfix">' . $medias . ' </ul>';
            $html .= '</div></div>';
        }
        return $html;
    }

    /**
     * 如果名称在模型中,则id表示为module_name, name表示为module[name]
     * 如Article的title字段, id = article_title, name = article[name]
     */
    private function get_input_module($name, $module = '') {
        $id_value = $name;
        $name_value = $name;
        if (!empty($module)) {
            $id_value = $module . '_' . $name;
            $name_value = $module . '[' . $name .']';
        }
        return ['id' => $id_value, 'name' => $name_value];
    }
}
namespace App\Form;

class MyForm extends BaseFormGroup {
    /**
     * 普通输入框
     */
    public function text_field($label, $name, $value = '', $module = '', $required = false, $helper = '') {
        $icon = 'pencil';
        $field = $this->text_input_field($name, $module, $label, $value);
        $html = $this->form_group_box($label, $icon, $field, $helper, $required);
        echo $html;
    }

    /**
     * textarea输入框
     */
    public function textarea_field($label, $name, $value = '', $module = '', $required = false, $helper = '') {
        $icon = '';
        $field = $this->textarea_input_field($name, $module, $label, $value);
        $html = $this->form_group_box($label, $icon, $field, $helper, $required);
        echo $html;
    }

     /**
     * 手机输入框
     */
    public function phone_field($label, $name, $value = '', $module = '', $required = false, $helper = '') {
        $icon = 'mobile-phone';
        $field = $this->text_input_field($name, $module, $label, $value);
        $html = $this->form_group_box($label, $icon, $field, $helper, $required);
        echo $html;
    }

     /**
     * 数字输入框
     */
    public function number_filed($label, $name, $value = '', $module = '', $required = false, $helper = '') {
        $icon = '';
        $field = $this->number_input_field($name, $module, $label, $value);
        $html = $this->form_group_box($label, $icon, $field, $helper, $required);
        echo $html;
    }

     /**
     * email输入框
     */
    public function email_filed($label, $name, $value = '', $module = '', $required = false, $helper = '') {
        $icon = 'envelope-o';
        $field = $this->text_input_field($name, $module, $label, $value);
        $html = $this->form_group_box($label, $icon, $field, $helper, $required);
        echo $html;
    }

     /**
     * 超链接输入框
     */
    public function url_filed($label, $name, $value = '', $module = '', $required = false, $helper = '') {
        $icon = 'internet-explorer';
        $field = $this->text_input_field($name, $module, $label, $value);
        $html = $this->form_group_box($label, $icon, $field, $helper, $required);
        echo $html;
    }

     /**
     * 编辑器
     */
    public function editor_filed($label, $name, $value = '', $module = '', $required = false, $helper = '') {
        $icon = '';
        $field = $this->editor_input_field($name, $module, $label, $value);
        $html = $this->form_group_box($label, $icon, $field, $helper, $required);
        echo $html;
    }

    /**
     * 上传图片
     */
    public function image_field($label, $name, $value = '', $module = '', $required = false, $helper = '') {
        $icon = 'file-image-o';
        $medias = '';
        if (!empty($value)) {
            $media_list = '<li><div class="img-info"><img src="'. $value .'" /><p>'. $value .'</p></div></li>';
            $medias .= $media_list;
        }
        $field = $this->file_input_field($name, $module, $label, $value);
        $html = $this->media_form_group_box($medias, $label, $icon, $field, $helper, $required);
        echo $html;
    }
    /**
     * 上传视频
     */
    public function video_field($label, $name, $value = '', $module = '', $required = false, $helper = '') {
        $icon = 'file-video-o';
        $medias = '';
        if (!empty($value)) {
            $media_list = '<li><div class="img-info"><vidoe src="'. $value .'"></video><p>'. $value .'</p></div></li>';
            $medias .= $media_list;
        }
        $field = $this->file_input_field($name, $module, $label, $value);
        $html = $this->media_form_group_box($medias, $label, $icon, $field, $helper, $required);
        echo $html;
    }
    /**
     * 多图上传
     */
    public function multiple_field($label, $name, $value = [], $module = '', $required = false, $helper = '') {
        $icon = 'copy';
        $medias = '';
        if (!empty($value)) {
            foreach($value as $val) {
                $media_list = '<li><div class="img-info"><img src="'. $value .'" /><p>'. $value .'</p></div></li>';
                $medias .= $media_list;
            }
        }
        $field = $this->multiple_file_input_field($name, $module, $label, $value);
        $html = $this->media_form_group_box($medias, $label, $icon, $field, $helper, $required);
        echo $html;
    }

    /**
     * 下拉列表
     */
    public function select_list($label, $name, array $data,  $value = '', $module = '', $required = false, $helper = '') {
        $icon = 'align-justify';
        $field = $this->select_field($name, $module, $value, $data);
        $html = $this->form_group_box($label, $icon, $field, $helper, $required);
        echo $html;
    }
    /**
     * 单选
     */
    public function radio_list($label, $name, array $data, $value = '', $module = '', $required = false, $helper = '') {
        $icon = '';
        $field = $this->radio_field($name, $module, $value, $data);
        $html = $this->form_group_box($label, $icon, $field, $helper, $required);
        echo $html;
    }

}

猜你喜欢

转载自blog.csdn.net/tang05709/article/details/81481786