1. 程式人生 > >laravel自定義表單再次改進

laravel自定義表單再次改進

namespace App\Form;

class BaseFormGroup {
    
    /**
     * 普通輸入框
     */
    public function input_field($name, $value,  $placeholder = '', $data_html = []) {
        $data_html_str = $this->exec_data_html($data_html);
        return '<input type="text" id="'. $name .'" name="'. $name .'" value="'. $value .'" class="form-control" '. $data_html_str .' placeholder="'. $placeholder.'">';
    }
    /**
     * 數字輸入框
     */
    public function number_field($name, $value,  $placeholder = '', $data_html = []) {
        $data_html_str = $this->exec_data_html($data_html);
        return '<input type="text" id="'. $name .'" name="'. $name .'" value="'. $value .'" class="form-control" '. $data_html_str .' placeholder="'. $placeholder.'" style="width: 150px">';
    }
    /**
     * 單檔案上傳框
     */
    public function file_field($name, $value,  $placeholder = '', $data_html = []) {
        $data_html_str = $this->exec_data_html($data_html);
        return '<input type="file" id="'. $name .'" name="'. $name .'" value="'. $value .'" class="form-control" '. $data_html_str .' placeholder="'. $placeholder.'">';
    }
    /**
     * 多檔案上傳框
     */
    public function multiple_file_field($name, $value,  $placeholder = '', $data_html = []) {
        $data_html_str = $this->exec_data_html($data_html);
        return '<input type="file" id="'. $name .'" name="'. $name .'" value="'. $value .'" class="form-control" '. $data_html_str .' placeholder="'. $placeholder.'" multiple>';
    }
     /**
     * textarea
     */
    public function textarea_field($name, $value,  $placeholder = '', $data_html = []) {
        $data_html_str = $this->exec_data_html($data_html);
        return '<textarea id="'. $name .'" name="'. $name .'" class="form-control" '. $data_html_str .'>'. $value .'</textarea>';
    }
     /**
     * wangeditor
     */
    public function wangeditor_field($name, $value,  $placeholder = '', $data_html = []) {
        $data_html_str = $this->exec_data_html($data_html);
        $html = '<div id="'. $name .'" class="wangeditor-control" '. $data_html_str .'>'. $value .'</div>';
        $html .= '<input type="hidden" name="'. $name .'" class="wangeditor_value" />';
        return $html;
    }
    /**
     * 下拉框
     * data格式: [key => value]
     */
    public function select_field($name, $value, $data, $data_html = []) {
        $data_html_str = $this->exec_data_html($data_html);
        $html = '<select id="'. $name .'" name="'.$name.'" class="form-control" '. $data_html_str .'>';
        $html .= '<option value="">請選擇</option>';
        foreach($data as $key => $val) {
            if($key == $value) {
                $html .= '<option value="'. $key .'" selected="selected">'. $val .'</option>';
            } else {
                $html .= '<option value="'. $key .'">'. $val .'</option>';
            }
        }
        $html .= '</select>';
        return $html;
    }
    /**
     * radio
     * data格式: [key => value]
     */
    public function radio_field($name, $value, $data, $data_html = []) {
        $data_html_str = $this->exec_data_html($data_html);
        $html = '<div '. $data_html_str.'>';
        foreach($data as $key => $val) {
            if($key == $value) {
                $html .= '<label class="radio-inline"><input type="radio" id="'. $name . '_' . $key.'" name="'. $name .'"  value="'. $key .'" checked="checked">'. $val .'</option></label>';
            } else {
                $html .= '<label class="radio-inline"><input type="radio" id="'. $name . '_' . $key.'" name="'. $name .'"  value="'. $key .'">'. $val .'</option></label>';
            }
        }
        $html .= '</div>';
        return $html;
    }
    /**
     * checkbox
     * data格式: [key => value]
     */
    public function checkbox_field($name, $value, $data, $data_html = []) {
        $data_html_str = $this->exec_data_html($data_html);
        $html = '<div '. $data_html_str.'>';
        foreach($data as $key => $val) {
            if($key == $value) {
                $html .= '<label class="checkbox-inline"><input type="checkbox" id="'. $name . '_' . $key.'" name="'. $name .'"  value="'. $key .'" checked="checked">'. $val .'</option></label>';
            } else {
                $html .= '<label class="checkbox-inline"><input type="checkbox" id="'. $name . '_' . $key.'" name="'. $name .'"  value="'. $key .'">'. $val .'</option></label>';
            }
        }
        $html .= '</div>';
        return $html;
    }
    /**
     * 建立表單快
     */
    public function create_form_group($label_content, $field_content) {
        return '<div class="form-group">'. $label_content . $field_content .'</div>';
    }
    /**
     * label標籤
     */
    public function label_html($label, $required = false, $data_html = []) {
        $data_html_str = $this->exec_data_html($data_html);
        if($required) {
            $label .= ' <span>*</span>';
        }
        return '<label class="col-sm-2 control-label" '. $data_html_str .'>'. $label .'</label>';
    }
    /**
     * 表單域
     */
    public function field_html($name, $field_html, $icon = '', $errors = '', $helper = '') {
        $icon_html = '';
        $helper_html = '';
        $error_html = '';
        if(!empty($icon)) {
            $icon_html = $this->icon_html($icon);
        }
        if(!empty($helper)) {
            $helper_html = $this->helper_html($helper);
        }
        if(!empty($errors)) {
            $error_html = $this->error_html($name, $errors);
        }
        $html = '<div class="col-sm-8">'. $error_html .'<div class="input-group">';
        $html .= $icon_html . $field_html . '</div>'. $helper_html .'</div>';
        return $html;
    }
    /**
     * 輸入框圖示
     */
    public function icon_html($icon) {
        return '<span class="input-group-addon"><i class="fa fa-'. $icon .' fa-fw"></i></span>';
    }
    /**
     * 表單錯誤提示
     */
    public function error_html($key, $errors) {
        $html = '';
        if(!empty($errors)) {
            if($errors->has($key)) {
                $html .= '<label class="control-label error-label">';
                foreach ($errors->get($key) as $message) {
                    $html .= $message .'&nbsp;&nbsp;';
                }
                $html .= '</label>';
            }
        }
        return $html;
    }
    /**
     * 表單幫助提示
     */
    public function helper_html($helper) {
        return '<p class="help-block">'. $helper .'</p>';
    }
    /**
     * 媒體顯示
     * medias格式: [path => src]
     * type: image video
     */
    public function media_list($medias, $type = 'image') {
        $html = '<div class="row media-list"><div class="col-sm-2"> &nbsp;</div> <div class="col-sm-8"><ul class="clearfix">';
        foreach($medias as $data) {
            $html .= '<li><div class="img-info">';
            if($type == 'video') {
                $html .= '<video src="'. env('APP_URL') . $data .'"></video';
            } else {
                $html .= '<img src="'. env('APP_URL') . $data .'" />';
            }
            $html .= '<p>'. $data .'</p>';
            $html .= ' </div></li>';
        }
        $html .= '</ul></div></div>';
        return $html;
    }
    /**
     * data_html 主裝為 data- 
     * 格式: ['title' => title, 'data-id' => id]
     */
    protected function exec_data_html ($data_html = []) {
        $html = '';
        if (!empty($data_html)) {
            foreach($data_html as $key => $val) {
                $html .= 'data-' . $key . '=' . $val;
            }
        }
        return $html;
    }
}
namespace App\Form;

class MyForm extends BaseFormGroup {
    
    private $model;
    /**
     * 建立表單
     */
    public function open_form($model, $url, $method='POST', $data_html = []) {
        $this->model = $model;
        $data_html_str = $this->exec_data_html($data_html);
        return '<form class="form-horizontal" name="theForm" action="'. $url .'" method="'. $method .'" role="form" '. $data_html_str .'>';
    }
    /**
     * 表單結束
     */
    public function close_form() {
        return '</form>';
    }
    /**
     * 提交表單
     */
    public function submit_form() {
        $label_content = '<label class="col-sm-2 control-label">&nbsp;</label>';
        $field_content = '<div class="col-sm-8">';
        $field_content .= '<div class="pull-left reset-btn"><input type="reset" class="btn btn-default" value="重 置" /></div>';
        $field_content .= '<div class="pull-right submit-btn"><input type="submit" class="btn btn-info" value="提 交" /></div>';
        $field_content .= '</div>';
        $form_group = $this->create_form_group($label_content, $field_content);
        return $form_group;
    }
    /**
     * 普通文字表單
     */
    public function normal_text_input($name, $errors = '', $label = '', $placeholder = '', $helper = '', $label_data = [], $field_data = []) {
        $label = empty($label) ? __('label.'.$name) : $label;
        $placeholder = empty($placeholder) ? __('placeholder.'.$name) : $placeholder;
        $required = array_key_exists($name, $this->model::$rules) ? true : false;

        $label_content = $this->label_html($label, $required, $label_data);
        $value = $this->model->$name;
        $field_html = $this->input_field($name, $value, $placeholder, $field_data);
        $field_content = $this->field_html($name, $field_html, 'pencil', $errors, $helper);

        $html = $this->create_form_group($label_content, $field_content);
        return $html;
    }
    /**
     * 數字表單
     */
    public function number_text_input($name, $errors = '', $label = '', $placeholder = '', $helper = '', $label_data = [], $field_data = []) {
        $label = empty($label) ? __('label.'.$name) : $label;
        $placeholder = empty($placeholder) ? __('placeholder.'.$name) : $placeholder;
        $required = array_key_exists($name, $this->model::$rules) ? true : false;

        $label_content = $this->label_html($label, $required, $label_data);
        $value = $this->model->$name;
        $field_html = $this->number_field($name, $value, $placeholder, $field_data);
        $field_content = $this->field_html($name, $field_html, 'pencil', $errors, $helper);

        $html = $this->create_form_group($label_content, $field_content);
        return $html;
    }
    /**
     * 郵箱表單
     */
    public function email_text_input($name, $errors = '', $label = '', $placeholder = '', $helper = '', $label_data = [], $field_data = []) {
        $label = empty($label) ? __('label.'.$name) : $label;
        $placeholder = empty($placeholder) ? __('placeholder.'.$name) : $placeholder;
        $required = array_key_exists($name, $this->model::$rules) ? true : false;

        $label_content = $this->label_html($label, $required, $label_data);
        $value = $this->model->$name;
        $field_html = $this->input_field($name, $value, $placeholder, $field_data);
        $field_content = $this->field_html($name, $field_html, 'envelope-o', $errors, $helper);

        $html = $this->create_form_group($label_content, $field_content);
        return $html;
    }
    /**
     * 電話表單
     */
    public function phone_text_input($name, $errors = '', $label = '', $placeholder = '', $helper = '', $label_data = [], $field_data = []) {
        $label = empty($label) ? __('label.'.$name) : $label;
        $placeholder = empty($placeholder) ? __('placeholder.'.$name) : $placeholder;
        $required = array_key_exists($name, $this->model::$rules) ? true : false;

        $label_content = $this->label_html($label, $required, $label_data);
        $value = $this->model->$name;
        $field_html = $this->input_field($name, $value, $placeholder, $field_data);
        $field_content = $this->field_html($name, $field_html, 'mobile-phone', $errors, $helper);

        $html = $this->create_form_group($label_content, $field_content);
        return $html;
    }
    /**
     * 連線表單
     */
    public function url_text_input($name, $errors = '', $label = '', $placeholder = '', $helper = '', $label_data = [], $field_data = []) {
        $label = empty($label) ? __('label.'.$name) : $label;
        $placeholder = empty($placeholder) ? __('placeholder.'.$name) : $placeholder;
        $required = array_key_exists($name, $this->model::$rules) ? true : false;

        $label_content = $this->label_html($label, $required, $label_data);
        $value = $this->model->$name;
        $field_html = $this->input_field($name, $value, $placeholder, $field_data);
        $field_content = $this->field_html($name, $field_html, 'internet-explorer', $errors, $helper);

        $html = $this->create_form_group($label_content, $field_content);
        return $html;
    }
     /**
     * textarea表單
     */
    public function textarea_input($name, $errors = '', $label = '', $placeholder = '', $helper = '', $label_data = [], $field_data = []) {
        $label = empty($label) ? __('label.'.$name) : $label;
        $placeholder = empty($placeholder) ? __('placeholder.'.$name) : $placeholder;
        $required = array_key_exists($name, $this->model::$rules) ? true : false;

        $label_content = $this->label_html($label, $required, $label_data);
        $value = $this->model->$name;
        $field_html = $this->textarea_field($name, $value, $placeholder, $field_data);
        $field_content = $this->field_html($name, $field_html, '', $errors, $helper);

        $html = $this->create_form_group($label_content, $field_content);
        return $html;
    }
    /**
     * 單圖表單
     */
    public function image_input($name, $errors = '', $label = '', $placeholder = '', $helper = '', $label_data = [], $field_data = []) {
        $label = empty($label) ? __('label.'.$name) : $label;
        $placeholder = empty($placeholder) ? __('placeholder.'.$name) : $placeholder;
        $required = array_key_exists($name, $this->model::$rules) ? true : false;

        $label_content = $this->label_html($label, $required, $label_data);
        $value = $this->model->$name;
        $media_html = '';
        if(!empty($value)) {
            $medias = [$value];
            $media_html = $this->media_list($medias);
        }
        $label_content = $media_html . $label_content;
        $field_html = $this->file_field($name, $value, $placeholder, $field_data);
        $field_content = $this->field_html($name, $field_html, 'file-image-o', $errors, $helper);

        $html = $this->create_form_group($label_content, $field_content);
        return $html;
    }
    /**
     * 視訊表單
     */
    public function video_input($name, $errors = '', $label = '', $placeholder = '', $helper = '', $label_data = [], $field_data = []) {
        $label = empty($label) ? __('label.'.$name) : $label;
        $placeholder = empty($placeholder) ? __('placeholder.'.$name) : $placeholder;
        $required = array_key_exists($name, $this->model::$rules) ? true : false;

        $label_content = $this->label_html($label, $required, $label_data);
        $value = $this->model->$name;
        $media_html = '';
        if(!empty($value)) {
            $medias = [$value];
            $media_html = $this->media_list($medias, 'video');
        }
        $label_content = $media_html . $label_content;
        $field_html = $this->file_field($name, $value, $placeholder, $field_data);
        $field_content = $this->field_html($name, $field_html, 'video-camera', $errors, $helper);

        $html = $this->create_form_group($label_content, $field_content);
        return $html;
    }
    /**
     * 視訊表單
     */
    public function content_input($name, $errors = '', $label = '', $placeholder = '', $helper = '', $label_data = [], $field_data = []) {
        $label = empty($label) ? __('label.'.$name) : $label;
        $placeholder = empty($placeholder) ? __('placeholder.'.$name) : $placeholder;
        $required = array_key_exists($name, $this->model::$rules) ? true : false;

        $label_content = $this->label_html($label, $required, $label_data);
        $value = $this->model->$name;
        $field_html = $this->wangeditor_field($name, $value, $placeholder, $field_data);
        $field_content = $this->field_html($name, $field_html, '', $errors, $helper);

        $html = $this->create_form_group($label_content, $field_content);
        return $html;
    }
    /**
     * 下拉表單
     */
    public function select_input($name, $data, $errors = '', $label = '',  $helper = '', $label_data = [], $field_data = []) {
        $label = empty($label) ? __('label.'.$name) : $label;
        $required = array_key_exists($name, $this->model::$rules) ? true : false;

        $label_content = $this->label_html($label, $required, $label_data);
        $value = $this->model->$name;
        $field_html = $this->select_field($name, $value, $data, $field_data);
        $field_content = $this->field_html($name, $field_html, '', $errors, $helper);

        $html = $this->create_form_group($label_content, $field_content);
        return $html;
    }
    /**
     * radio
     */
    public function radio_input($name, $data, $errors = '', $label = '',  $helper = '', $label_data = [], $field_data = []) {
        $label = empty($label) ? __('label.'.$name) : $label;
        $required = array_key_exists($name, $this->model::$rules) ? true : false;

        $label_content = $this->label_html($label, $required, $label_data);
        $value = $this->model->$name;
        $field_html = $this->radio_field($name, $value, $data, $field_data);
        $field_content = $this->field_html($name, $field_html, '', $errors, $helper);

        $html = $this->create_form_group($label_content, $field_content);
        return $html;
    }
    /**
     * check_box
     */
    public function checkbox_input($name, $data, $errors = '', $label = '',  $helper = '', $label_data = [], $field_data = []) {
        $label = empty($label) ? __('label.'.$name) : $label;
        $required = array_key_exists($name, $this->model::$rules) ? true : false;

        $label_content = $this->label_html($label, $required, $label_data);
        $value = $this->model->$name;
        $field_html = $this->checkbox_field($name, $value, $data, $field_data);
        $field_content = $this->field_html($name, $field_html, '', $errors, $helper);

        $html = $this->create_form_group($label_content, $field_content);
        return $html;
    }

}
{!! MyFormFacade::open_form($model, url('backend/articles')) !!}
    {{ csrf_field() }}
    <div class="box-body">
    {!! MyFormFacade::normal_text_input('title', $errors) !!}
    {!! MyFormFacade::select_input('category_id', $tree, $errors) !!}
    {!! MyFormFacade::image_input('image') !!}
    {!! MyFormFacade::video_input('video') !!}
    {!! MyFormFacade::normal_text_input('seo_title') !!}
    {!! MyFormFacade::normal_text_input('seo_keywords') !!}
    {!! MyFormFacade::textarea_input('seo_description') !!}
    {!! MyFormFacade::content_input('content', $errors) !!}
    {!! MyFormFacade::number_text_input('order') !!}
    </div>
    <div class="clearfix box-footer">
    {!! MyFormFacade::submit_form() !!}
    </div>
    
    {!! MyFormFacade::close_form() !!}

相關推薦

laravel定義再次改進

namespace App\Form; class BaseFormGroup { /** * 普通輸入框 */ public function input_field($name, $value, $placeholder

Laravel 5.5 FormRequest 定義請求驗證類

根目錄 use his extends 數據 不一致 不能 登錄模塊 能夠 1、把表單驗證邏輯寫在Controller中,這是最基礎的方法,但是不好維護,如: 1 namespace App\Http\Controllers\Admin; 2 3 use Illu

laravel-admin(定義與驗證)

ret 技術 UNC acad ida 建表 自定義表單 class 分享圖片 場景: 很多時候,由於我們業務場景比較特殊,需要自定義表單,然後框架給我提供了對應表單組建! 案列:以創建一個字段為列 1.在控制器對應的方法中調用表單組建創建表單 public

java sql編輯器 動態報表 數據庫備份還原 quartz定時任務調度 定義 SSM

大小 demo 結構 圖片顯示 登錄 效果 tab 雙向 mar A 調用攝像頭拍照,自定義裁剪編輯頭像,頭像圖片色度調節B 集成代碼生成器 [正反雙向](單表、主表、明細表、樹形表,快速開發利器)+快速表單構建器 freemaker模版技術 ,0個代碼不用寫,生成完整的一

定義流程gooflow2.0+定義

log ges bapi cnblogs 語句 參與者 源碼 -1 通過 一、功能簡介 gooflow功能清單1、自定義流程繪制2、自定義屬性添加3、支持3種步驟類型普通審批步驟自動決策步驟手動決策步驟 4、決策方式(支持js決策,sql語句決策) 5、審批人員參與方

java sql編輯器 動態報表 數據庫備份還原 quartz定時任務調度 定義 java圖片爬蟲

springmvcmybatis整合 bootstrap 框架源碼 spring html5 官網 http://www.fhadmin.org/A代碼編輯器,在線模版編輯,仿開發工具編輯器,pdf在線預覽,文件轉換編碼B 集成代碼生成器 [正反雙向](單表、主表、明細表、樹形表,快速開

java sql 編輯器 數據庫備份還原 quartz 定時任務調度 定義 java 圖片爬蟲 集成代碼生成器

分離 文件大小 發送郵件 進度條 服務器配置 mysql http 備份數據庫 生成報表 A代碼編輯器,在線模版編輯,仿開發工具編輯器,pdf在線預覽,文件轉換編碼B 集成代碼生成器 [正反雙向](單表、主表、明細表、樹形表,快速開發利器)+快速表單構建器 freemake

下載基於LigerUI+JBPM5定義+Node.js的J2EE大型金融項目《財務預算系統》開發全程實錄

財務預算系統 大型金融項目 jbpm5自定義表單 基於LigerUI+JBPM5自定義表單+Node.js的J2EE大型金融項目《財務預算系統》開發全程實錄地址:http://pan.baidu.com/s/1eRBSHGY 密碼:q8x8課程分為三季:第一季:基礎篇,主要內容包括:基礎設置,主要

織夢後臺定義,中文不顯示,數字和字母顯示的解決辦法

數字和字母 如果 PE spa templet lsp 文件 html fields 1.找到 找到dede/templets/diy_list.htm 這個文件。 2.找到htmlspecialchars($fields[$field]); 這個位置,請在工具

Winform定義(轉)

serialize ria tro 修改 bench ron att design control 出處:http://www.newlifex.com/showtopic-167.aspx 好吧,附件真的損壞了,原始代碼我也沒有了,再提取我也沒精力了,不好意思,哪位

dede定義放首頁出錯的解決辦法

TP 技術 定義 dede 檢查 解決辦法 意思 mage 怎麽 一、當自定義表單放首頁提交的時候跳出這個頁面怎麽解決 二、解決辦法 可能有多個from表單提交出錯,也就是代碼沖突的意思,只要把代碼檢查好,from提交不要重復沖突就可以了 dede自定義表單放首頁出

織夢定義ajax提交

不寫死任何東西,藉助jquery ajax提交dedecms自定義表單到後臺。 注意表單部分,此例只做為參考,實際專案中根據自己的情況酌情修改。 認真看下面例子中 紅色部分 <form action="/plus/diy.php" enctype="multipart/form-data" me

織夢後臺定義聯動地區顯示為數字的真正解決方法

tar borde ret get isset func execute fun 也有 織夢聯動地區後臺顯示為數字的真正解決方法,其實官方也有,但是不能直接調用,我把它提取出來單獨寫個在後臺自定義表單裏 打開 /dede/templets/diy_list.htm 找到 (

織夢定義用js代替聯動地區解決聯動地區選擇

span tex 選擇 字段 pan java htm img czc 用js+文本字段實現全國城市三級聯動 1、在自定義表單中添加 三個字段,字段類型 單行文本(varchar) 省份s_province 城市s_city 地區s_county 2、前臺模板中自定義表單

織夢定義前臺模版顯示+分頁調用

charset 新建 自定義函數 part 1.0 return tmp glob order by 根目錄建立form.php <?php require_once(dirname(__FILE__)."/../include/common.inc.php"); r

織夢定義新增訪客提交時間和訪客IP+限制每天每個IP提交次數

織夢給自定義表單新增訪客提交時間 不需要在模板htm裡新增js或者其他程式碼,按下面步驟來即可。 1、後臺 - 核心 - 頻道模型 - 自定義表單 - 新增新欄位 提交時間 time 單行文字(varchar) 2、開啟 /plus/diy.php 找到 $fiel

織夢後臺定義新增搜尋功能

自定義表單搜尋效果圖 當你的自定義表單資料過多或者需要查詢某個單時,給後臺的自定義表單加個簡單的搜尋功能很有必要。 1、開啟 \dede\templets\diy_list.htm 找到內容列表</div>在它的下面加入01 <div class="bodytitletxt" style

vue+element中定義校驗特殊字元

本次專案基於vue和element-ui,需要在前端使用者輸入的時候去校驗輸入的內容中是否含有特殊符號,如果有,則提示使用者不支援輸入特殊符號。 校驗規則方法 export function checkSpecificKey(str) { var special

DedeCMS實現定義提交後傳送指定郵箱的方法

連結: https://pan.baidu.com/s/1t7F8eah3Bw-97jsKNqiQQg 提取碼: gs6j  一、登陸QQ郵箱——設定——賬戶,找到POP3/IMAP/SMTP,開啟,現在的郵箱開啟POP3/SMTP都要授權碼,記下你的授權碼,等一下是作為密碼使用的

資料檢驗外掛: Validate外掛 新增定義驗證

        jQuery.validator.addMethod("ValiPass", function(value, element,params) {         var exp =