PHP實(shí)現(xiàn)動(dòng)態(tài)表單生成工具詳解
表單包含多種input類(lèi)型,包括 hiiden類(lèi)型 ,text類(lèi)型,radio類(lèi)型,checkbox類(lèi)型,textarea類(lèi)型,file類(lèi)型,select類(lèi)型等基礎(chǔ)類(lèi)型,手寫(xiě)表單就是累耗時(shí)耗力開(kāi)發(fā)銷(xiāo)量太低而且代碼量大了還容易寫(xiě)出bug,每個(gè)頁(yè)面的表單遇到改動(dòng)的時(shí)候恨不得長(zhǎng)十雙手去改,于是我自己開(kāi)發(fā)了一個(gè)php寫(xiě)的表單生成工具,在業(yè)務(wù)邏輯通過(guò)配置或者鏈?zhǔn)讲僮魅コ跏急韱谓Y(jié)構(gòu)和數(shù)據(jù),不管有多少頁(yè)面都只需要通過(guò)配置下就行,我寫(xiě)的這個(gè)表單工具 支持塊表單,行內(nèi)表單,table類(lèi)型表單,支持表單美化 默認(rèn)是layui效果,也支持jquery控制表單行為,只需要引入layui樣式和js即可。在github主頁(yè)demo目錄也提供了示例代碼運(yùn)行查看效果,
Form介紹
使用php編寫(xiě)的html表單生成工具,沒(méi)有任何依賴(lài)可獨(dú)立使用,支持鏈?zhǔn)讲僮骱团渲脛?chuàng)建表單,支持表單美化(默認(rèn)為layui風(fēng)格)
特點(diǎn)
- 沒(méi)有任何依賴(lài)可獨(dú)立使用
- 支持鏈?zhǔn)讲僮鲃?chuàng)建表單
- 支持?jǐn)?shù)組配置創(chuàng)建表單
- 支持塊表單
- 支持行內(nèi)表單
- 支持table表單
- 支持表單美化(默認(rèn)為layui風(fēng)格)且方便擴(kuò)展
項(xiàng)目主頁(yè)鏈接
github: https://github.com/mgckid/form
gitee:https://gitee.com/mgckid/form
安裝方法
composer require mgckid/form
快速使用
鏈?zhǔn)讲僮鲃?chuàng)建塊表單

<?php
require __DIR__ . '/../src/Form.php';
Form::getInstance()
->form_method(Form::form_method_post)
->form_action('/')
->input_text('姓名', '', 'name', '法外狂徒張三')
->radio('性別', '', 'male', ['male' => '男', 'female' => '女'], 'male')
->checkbox('愛(ài)好', '', 'interest', ['ktv' => 'K歌', 'dance' => '跳舞', 'movie' => '看電影', 'run' => '跑步'], 'ktv,run')
->input_inline_start()
->input_text('省份', '', 'sheng', '湖北省')
->input_text('市', '', 'shi', '武漢市')
->input_text('區(qū)', '', 'qu', '武昌區(qū)')
->input_text('街道', '', 'jie', '紫陽(yáng)路36號(hào)')
->input_inline_end()
->input_hidden('id', '1')
->input_text('user name', '', 'user', 'admin')
->input_password('password', '', 'password', '123456')
->radio('is active', '', 'is_active', [
['value' => '1', 'name' => 'active'],
['value' => '0', 'name' => 'unactive']
], 1)
->checkbox('user role', '', 'role', [
['value' => '1', 'name' => 'boss'],
['value' => '2', 'name' => 'manager'],
['value' => '3', 'name' => 'employee'],
], '1,2')
->select('user department', '', 'department', [
['value' => '1', 'name' => 'sales'],
['value' => '2', 'name' => 'hr'],
['value' => '3', 'name' => 'secured'],
], 1)
->form_class(LayuiForm::form_class_pane)
->input_submit('確認(rèn)保存', 'class="layui-btn" lay-submit lay-filter="saveBtn"')
//->input_date()
//->editor()
//->form_data()
//->table()
->create();
?>數(shù)組配置創(chuàng)建塊表單

<?php
require __DIR__ . '/../src/Form.php';
$init = array(
0 => array(
'title' => 'Id',
'name' => 'id',
'description' => 'Id',
'enum' => array(),
'type' => 'hidden',
'widget_type' => '',
),
1 => array(
'title' => '用戶(hù)id',
'name' => 'user_id',
'description' => '用戶(hù)id',
'enum' => array(),
'type' => 'hidden',
'widget_type' => '',
),
2 => array(
'title' => '用戶(hù)名',
'name' => 'username',
'description' => '用戶(hù)名',
'enum' => array(),
'type' => 'text',
'widget_type' => '',
),
3 => array(
'title' => '真實(shí)姓名',
'name' => 'true_name',
'description' => '真實(shí)姓名',
'enum' => array(),
'type' => 'text',
'widget_type' => '',
),
4 => array(
'title' => '密碼',
'name' => 'password',
'description' => '密碼',
'enum' => array(),
'type' => 'text',
'widget_type' => '',
),
5 => array(
'title' => '郵箱',
'name' => 'email',
'description' => '郵箱',
'enum' => array(),
'type' => 'text',
'widget_type' => '',
),
6 => array(
'title' => '是否刪除',
'name' => 'deleted',
'description' => '是否刪除',
'enum' => array(
0 => '未刪除',
1 => '已刪除',
),
'type' => 'none',
'widget_type' => '',
),
7 => array(
'title' => '創(chuàng)建時(shí)間',
'name' => 'created',
'description' => '創(chuàng)建時(shí)間',
'enum' => array(),
'type' => 'none',
'widget_type' => 'date',
),
8 => array(
'title' => '修改時(shí)間',
'name' => 'modified',
'description' => '修改時(shí)間',
'enum' => array(),
'type' => 'none',
'widget_type' => 'date',
),
);
$data = array(
'id' => 2,
'user_id' => 'feac0fa3-3245-11e6-9b90-e03f49a02407',
'username' => 'admin',
'true_name' => '系統(tǒng)管理員',
'email' => '',
'deleted' => 0,
'created' => '2016-06-14 23:39:52',
'modified' => '2020-03-12 20:07:48',
);
\Form::getInstance()
->form_schema($init)
->form_data($data)
->input_submit('確認(rèn)保存', 'class="layui-btn" lay-submit lay-filter="saveBtn"')
->create();行內(nèi)表單

<?php
require __DIR__ . '/../src/Form.php';
$init = array(
0 => array(
'title' => 'Id',
'name' => 'id',
'description' => 'Id',
'enum' => array(),
'type' => 'hidden',
'widget_type' => '',
),
1 => array(
'title' => '用戶(hù)id',
'name' => 'user_id',
'description' => '用戶(hù)id',
'enum' => array(),
'type' => 'text',
'widget_type' => '',
),
2 => array(
'title' => '用戶(hù)名',
'name' => 'username',
'description' => '用戶(hù)名',
'enum' => array(),
'type' => 'text',
'widget_type' => '',
),
3 => array(
'title' => '真實(shí)姓名',
'name' => 'true_name',
'description' => '真實(shí)姓名',
'enum' => array(),
'type' => 'text',
'widget_type' => '',
),
4 => array(
'title' => '密碼',
'name' => 'password',
'description' => '密碼',
'enum' => array(),
'type' => 'text',
'widget_type' => '',
),
5 => array(
'title' => '郵箱',
'name' => 'email',
'description' => '郵箱',
'enum' => array(),
'type' => 'text',
'widget_type' => '',
),
6 => array(
'title' => '是否刪除',
'name' => 'deleted',
'description' => '是否刪除',
'enum' => array(
0 => '未刪除',
1 => '已刪除',
),
'type' => 'none',
'widget_type' => '',
),
7 => array(
'title' => '創(chuàng)建時(shí)間',
'name' => 'created',
'description' => '創(chuàng)建時(shí)間',
'enum' => array(),
'type' => 'none',
'widget_type' => 'date',
),
8 => array(
'title' => '修改時(shí)間',
'name' => 'modified',
'description' => '修改時(shí)間',
'enum' => array(),
'type' => 'none',
'widget_type' => 'date',
),
);
\Form::getInstance()
->input_inline_start()
->form_schema($init)
->input_submit('<i class="layui-icon"></i> 搜索', ' class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"', 'class="layui-btn layui-btn-primary"')
->input_inline_end()
->form_class(\LayuiForm::form_class_pane)
->form_method(Form::form_method_get)
->create();table表單

<?php
require __DIR__ . '/../src/Form.php';
$form_init = array (
'id' =>array (
'title' => '主鍵',
'name' => 'id',
'description' => '主鍵',
'enum' =>array(),
'type' => 'hidden',
'widget_type' => '',
),
'name' =>array (
'title' => '配置名稱(chēng)',
'name' => 'name',
'description' => '配置名稱(chēng)',
'enum' =>array(),
'type' => 'text',
'widget_type' => '',
),
'description' =>array (
'title' => '配置描述',
'name' => 'description',
'description' => '配置描述',
'enum' =>array(),
'type' => 'text',
'widget_type' => '',
),
'input_type' =>array (
'title' => '表單類(lèi)型',
'name' => 'input_type',
'description' => '表單類(lèi)型',
'enum' =>array (
'hidden' => '隱藏域',
'select' => '下拉',
'radio' => '單選按鈕',
'text' => '文本',
'textarea' => '多行文本',
'file' => '上傳',
'none' => '非表單',
'editor' => '富文本',
'checkbox' => '復(fù)選框',
'date' => '日期',
),
'type' => 'select',
'widget_type' => '',
),
'created' =>array (
'title' => '創(chuàng)建時(shí)間',
'name' => 'created',
'description' => '創(chuàng)建時(shí)間',
'enum' =>array(),
'type' => 'none',
'widget_type' => 'date',
),
'modified' =>array (
'title' => '修改時(shí)間',
'name' => 'modified',
'description' => '修改時(shí)間',
'enum' =>array(),
'type' => 'none',
'widget_type' => 'date',
),
'deleted' =>array (
'title' => '刪除標(biāo)記',
'name' => 'deleted',
'description' => '刪除標(biāo)記',
'enum' =>array (
0 => '未刪除',
1 => '已刪除',
),
'type' => 'none',
'widget_type' => '',
),
);
$form_data=array (
0 =>
array (
'id' => 73,
'name' => 'solution_introduction',
'value' => '111',
'description' => '解決方案介紹',
'input_type' => 'textarea',
'created' => '2018-12-07 11:44:40',
'modified' => '2022-03-08 00:32:08',
'deleted' => 0,
),
1 =>
array (
'id' => 72,
'name' => 'tese_product_introduction',
'value' => '222',
'description' => '特色產(chǎn)品介紹',
'input_type' => 'textarea',
'created' => '2018-12-07 11:43:52',
'modified' => '2022-03-08 00:32:09',
'deleted' => 0,
),
2 =>
array (
'id' => 71,
'name' => 'new_product_introduction',
'value' => '333',
'description' => '新產(chǎn)品介紹',
'input_type' => 'textarea',
'created' => '2018-12-07 11:41:37',
'modified' => '2022-03-08 00:32:09',
'deleted' => 0,
),
3 =>
array (
'id' => 70,
'name' => 'site_pinterest',
'value' => '',
'description' => 'Pinterest堪稱(chēng)圖片版的Twitter鏈接',
'input_type' => 'text',
'created' => '2018-11-19 11:48:12',
'modified' => '2019-04-27 14:08:07',
'deleted' => 0,
),
4 =>
array (
'id' => 69,
'name' => 'site_twitter',
'value' => '',
'description' => 'Twitter(非官方漢語(yǔ)通稱(chēng)推特)鏈接',
'input_type' => 'text',
'created' => '2018-11-19 11:47:04',
'modified' => '2019-04-27 14:08:07',
'deleted' => 0,
),
5 =>
array (
'id' => 68,
'name' => 'site_facebook',
'value' => '',
'description' => 'Facebook(臉書(shū))鏈接',
'input_type' => 'text',
'created' => '2018-11-19 11:46:07',
'modified' => '2019-04-27 14:08:07',
'deleted' => 0,
),
6 =>
array (
'id' => 67,
'name' => 'site_google_plus',
'value' => '',
'description' => 'Google+SNS社交網(wǎng)站鏈接',
'input_type' => 'text',
'created' => '2018-11-19 11:45:26',
'modified' => '2019-04-27 14:08:07',
'deleted' => 0,
),
7 =>
array (
'id' => 66,
'name' => 'site_linkedin',
'value' => '',
'description' => 'LinkedIn領(lǐng)英鏈接',
'input_type' => 'text',
'created' => '2018-11-19 11:43:53',
'modified' => '2019-04-27 14:08:07',
'deleted' => 0,
),
8 =>
array (
'id' => 65,
'name' => 'site_livechat_code',
'value' => '',
'description' => 'livezilla在線客服代碼',
'input_type' => 'textarea',
'created' => '2018-11-15 16:45:15',
'modified' => '2019-04-27 14:08:07',
'deleted' => 0,
),
9 =>
array (
'id' => 64,
'name' => 'site_skype',
'value' => '',
'description' => '聯(lián)系skype',
'input_type' => 'text',
'created' => '2018-11-15 16:44:40',
'modified' => '2019-04-27 14:08:07',
'deleted' => 0,
)
);
\Form::getInstance()
->table('擴(kuò)展配置', '', 'site_config', $form_init, $form_data)
->create();以上就是PHP實(shí)現(xiàn)動(dòng)態(tài)表單生成工具詳解的詳細(xì)內(nèi)容,更多關(guān)于PHP表單生成工具的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
php版微信支付api.mch.weixin.qq.com域名解析慢原因與解決方法
這篇文章主要介紹了php版微信支付api.mch.weixin.qq.com域名解析慢原因與解決方法,詳細(xì)分析了微信支付api.mch.weixin.qq.com域名解析慢原因與使用curl_easy_setopt指定ipv4解決ipv6解析問(wèn)題的相關(guān)技巧,需要的朋友可以參考下2016-10-10
深入理解curl類(lèi),可用于模擬get,post和curl下載
本篇文章是對(duì)curl類(lèi),可用于模擬get,post和curl下載進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP中將字符串轉(zhuǎn)化為整數(shù)(int) intval() printf() 性能測(cè)試
早在Sql注入橫行的前幾年,字符串轉(zhuǎn)化為整數(shù)就已經(jīng)被列為每個(gè)web程序必備的操作了。web程序?qū)et或post來(lái)的id、整數(shù)等值強(qiáng)制經(jīng)過(guò)轉(zhuǎn)化函數(shù)轉(zhuǎn)化為整數(shù),過(guò)濾掉危險(xiǎn)字符,盡可能降低系統(tǒng)本身被Sql注入的可能性2012-03-03
探討如何在PHP開(kāi)啟gzip頁(yè)面壓縮實(shí)例
本篇文章是對(duì)PHP開(kāi)啟gzip頁(yè)面壓縮實(shí)例進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP面向?qū)ο蟪绦蛟O(shè)計(jì)模擬一般面向?qū)ο笳Z(yǔ)言中的方法重載(overload)示例
這篇文章主要介紹了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)模擬一般面向?qū)ο笳Z(yǔ)言中的方法重載(overload),結(jié)合具體實(shí)例形式分析了php模擬一般面向?qū)ο蟪绦蛟O(shè)計(jì)語(yǔ)言中方法重載的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-06-06
php實(shí)現(xiàn)與erlang的二進(jìn)制通訊實(shí)例解析
這篇文章主要介紹了php實(shí)現(xiàn)與erlang的二進(jìn)制通訊實(shí)例解析,需要的朋友可以參考下2014-07-07
php使用curl并發(fā)減少后端訪問(wèn)時(shí)間的方法分析
這篇文章主要介紹了php使用curl并發(fā)減少后端訪問(wèn)時(shí)間的方法,結(jié)合實(shí)例形式對(duì)比分析了curl使用并發(fā)訪問(wèn)前后程序運(yùn)行耗時(shí),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05
PHP截?cái)鄻?biāo)題且兼容utf8和gb2312編碼
PHP截?cái)鄻?biāo)題的方法有很多,但同時(shí)可以兼容utf8和gb2312卻不多了,下面有個(gè)不錯(cuò)的方法,可以參考下,或許對(duì)大家有所幫助2013-09-09

