Dynamically generated form implementation method
Use iview to dynamically create form forms
A form generator with data collection, verification and submission functions, including check boxes, radio buttons, input boxes, and drop-down selection boxes and other elements as well as time selection, date selection, color selection, and file/picture upload functions.
Example
Installation
npm install form-create
OR
git clone https://github.com/xaboy/form-create.git cd form-create npm install
Introduction
<!-- import Vue --> <script src="node_modules/vue/dist/vue.min.js"></script> <!-- import iview --> <link rel="stylesheet" href="node_modules/iview/dist/styles/iview.css"> <script src="node_modules/iview/dist/iview.min.js"></script> <!-- import formCreate --> <script src="dist/form-create.min.js"></script>
Use
let rules = [],el = document.body; new Vue({ mounted:function(){ var $f = this.$formCreate(rules,el); } })
$formCreate parameters
rules
Form rules [inputRule,selectRule,...]
el
The inserted Dom node defaults to document.body
$f instance method
Get form data
$f.formData()
Modify form data
$f.changeField(field,value)
Form validation
$f.validate(successFn, errorFn)
Form validation specified field
$f.validateField(field,errorFn)
Reset form
$f.resetFields()
Remove the form
$f.remove()
Get all fields of the form
$f.fields()
Submit form
$f.submit()
rules rules:
hidden hidden field
hiddenRule: { type:"hidden", field:"id", //字段名称 value:"14" //input值 }
input input box
inputRule : { type:"input", title:"商品名称", //label名称 field:"goods_name" , //字段名称 value:"iphone 7", //input值, props: { "type": "text", //输入框类型,可选值为 text、password、textarea、url、email、date "clearable":false, //是否显示清空按钮 "disabled": false, //设置输入框为禁用状态 "readonly": false, //设置输入框为只读 "rows": 4, //文本域默认行数,仅在 textarea 类型下有效 "autosize": false, //自适应内容高度,仅在 textarea 类型下有效,可传入对象,如 { minRows: 2, maxRows: 6 } "number": false, //将用户的输入转换为 Number 类型 "autofocus": false, //自动获取焦点 "autocomplete": "off", //原生的自动完成功能,可选值为 off 和 on "placeholder": "请输入商品名称", //占位文本 "size": "default", //输入框尺寸,可选值为large、small、default或者不设置, "spellcheck": false //原生的 spellcheck 属性 }, validate:[ { required: true, message: 'Mailbox cannot be empty', trigger: 'blur' }, { type: 'email', message: 'Incorrect email format', trigger: 'blur' } ] }
validate form validation rules, please view the detailed configuration: https://github.com/yiminghe/async- validator
radio radio button
radioRule : { type:"radio", title:"是否包邮", field:"is_postage", value:"0", //input值, options:[ {value:"0",label:"不包邮",disabled:false}, {value:"1",label:"包邮",disabled:true} ], props: { "type":undefined, //可选值为 button 或不填,为 button 时使用按钮样式 "size":"default", //单选框的尺寸,可选值为 large、small、default 或者不设置 "vertical":false //是否垂直排列,按钮样式下无效 }, validate:[] }
checkbox checkbox
checkboxRule : { type:"checkbox", title:"标签", field:"label", value:[ "1","2","3" ],//input值, options:[ {value:"1",label:"好用",disabled:true}, {value:"2",label:"方便",disabled:false}, {value:"3",label:"实用",disabled:false}, {value:"4",label:"有效",disabled:false} ], props: { "size":"default" //多选框组的尺寸,可选值为 large、small、default 或者不设置 }, validate:[] }
switch switch
switchRule : { type:"switch", title:"是否上架", field:"is_show", value:"1",//input值, props: { "size":"default", //开关的尺寸,可选值为large、small、default或者不写。建议开关如果使用了2个汉字的文字,使用 large。 "disabled":false,//禁用开关 "trueValue":"1", //选中时的值,当使用类似 1 和 0 来判断是否选中时会很有用 "falseValue":"0" //没有选中时的值,当使用类似 1 和 0 来判断是否选中时会很有用 }, slot: { open:"上架", //自定义显示打开时的内容 close:"下架" //自定义显示关闭时的内容 }, //slot可以不用配置 validate:[] }
select selector
selectRule : { type: "select", field: "cate_id", title: "产品分类", value: "104", //input值, multiple为true时 value为数组 [value,value,...] props: { "multiple": false, //是否支持多选 "clearable": false, //是否可以清空选项,只在单选时有效 "filterable": false, // 是否支持搜索 // 暂不支持远程搜索 // "remote": false, //是否使用远程搜索 // "remote-method":()=>{}, //远程搜索的方法 // "loading": false, //当前是否正在远程搜索 // "loading-text": "加载中", //远程搜索中的文字提示 "size":"default", //选择框大小,可选值为large、small、default或者不填 "placeholder": "请选择", //选择框默认文字 "not-found-text": "无匹配数据", //当下拉列表为空时显示的内容 "placement": "bottom", //弹窗的展开方向,可选值为 bottom 和 top "disabled": false //是否禁用 }, options: [ {"value": "104", "label": "生态蔬菜", "disabled": false}, {"value": "105", "label": "新鲜水果", "disabled": false} ] }
DatePicker date Selector
DatePickerRule : { type: "DatePicker", field: "section_day", title: "活动日期", value: 1519110955000 || new Date(), //input值, type为daterange,datetimerange value为数组 [start_value,end_value] props: { "type": "date", //显示类型,可选值为 date、daterange、datetime、datetimerange、year、month "format": "yyyy-MM-dd", //展示的日期格式 "placement": "bottom-start", // 日期选择器出现的位置,可选值为toptop-starttop-endbottombottom-startbottom-endleftleft-startleft-endrightright-startright-end "placeholder":"请选择获得时间", //占位文本 "confirm":false, //是否显示底部控制栏,开启后,选择完日期,选择器不会主动关闭,需用户确认后才可关闭 "size":"default", //尺寸,可选值为large、small、default或者不设置 "disabled":false, //是否禁用选择器 "clearable":true, //是否显示清除按钮 "readonly":false, //完全只读,开启后不会弹出选择器 "editable":false //文本框是否可以输入 }, validate:[] }
TimePicker Time Picker
TimePickerRule : { type: "TimePicker", field: "section_time", title: "活动时间", value: [], //input值, type为timerange value为数组 [start_value,end_value] props: { "type": "timerange", //显示类型,可选值为 time、timerange "format": "HH:mm:ss", //展示的时间格式 "steps": [], //下拉列表的时间间隔,数组的三项分别对应小时、分钟、秒。例如设置为 [1, 15] 时,分钟会显示:00、15、30、45。 "placement": "bottom-start", // 时间选择器出现的位置,可选值为toptop-starttop-endbottombottom-startbottom-endleftleft-startleft-endrightright-startright-end "placeholder":"请选择获得时间", //占位文本 "confirm":false, //是否显示底部控制栏,开启后,选择完日期,选择器不会主动关闭,需用户确认后才可关闭 "size":"default", //尺寸,可选值为large、small、default或者不设置 "disabled":false, //是否禁用选择器 "clearable":true, //是否显示清除按钮 "readonly":false, //完全只读,开启后不会弹出选择器 "editable":false //文本框是否可以输入 }, validate:[] }
InputNumber Number input box
InputNumberRule : { type: "InputNumber", field: "sort", title: "排序", value: 0, //input值 props: { "max": undefined, //最大值 "min": undefined, //最小值 "step": 1, //每次改变的步伐,可以是小数 "size":"default", //输入框尺寸,可选值为large、small、default或者不填 "disabled":false, //设置禁用状态 "readonly":false, //是否设置为只读 "editable":true, //是否可编辑 "precision":0 //数值精度 }, validate:[] }
ColorPicker Color Picker
ColorPickerRule : { type: "ColorPicker", field: "coloe", title: "颜色", value: '#ff7271', //input值 props: { "alpha": false, //是否支持透明度选择 "hue": true, //是否支持色彩选择 "recommend": false, //是否显示推荐的颜色预设 "size":"default", //尺寸,可选值为large、small、default或者不设置 "colors":[], //自定义颜色预设 "format":"hex" //颜色的格式,可选值为 hsl、hsv、hex、rgb,开启 alpha 时为 rgb,其它为 hex }, validate:[] }
Upload Upload
UploadRule : { type: "Upload", field: "pic", title: "轮播图", value: [], //input值 props: { "type":"select", //上传控件的类型,可选值为 select(点击选择),drag(支持拖拽) "action": "", //上传的地址,必填 "headers": {}, //设置上传的请求头部 "multiple": false, //是否支持多选文件 "data":{}, //上传时附带的额外参数 "name":"", //上传的文件字段名 "with-credentials":false, //支持发送 cookie 凭证信息 "show-upload-list":true, //是否显示已上传文件列表 "accept":"", //接受上传的文件类型 "format":[], //支持的文件类型,与 accept 不同的是,format 是识别文件的后缀名,accept 为 input 标签原生的 accept 属性,会在选择文件时过滤,可以两者结合使用 "max-size":undefined, //文件大小限制,单位 kb "beforeUpload":()=>{}, //上传文件之前的钩子,参数为上传的文件,若返回 false 或者 Promise 则停止上传 "onProgress":()=>{}, //文件上传时的钩子,返回字段为 event, file, fileList "onSuccess":( push, response, file, fileList)=>{ let filePath = response.data.filePath; push(filePath); //其他操作 }, //文件上传成功时的钩子,返回字段为 push, response, file, fileList. push(filePath) 将上传后的路径添加到value中 "onError":()=>{}, //文件上传失败时的钩子,返回字段为 error, file, fileList "onPreview":()=>{}, //点击已上传的文件链接时的钩子,返回字段为 file, 可以通过 file.response 拿到服务端返回数据 "onRemove":()=>{}, //文件列表移除文件时的钩子,返回字段为 file, fileList "onFormatError":()=>{}, //文件格式验证失败时的钩子,返回字段为 file, fileList "onExceededSize":()=>{}, //文件超出指定大小限制时的钩子,返回字段为 file, fileList "default-file-list":[ { name: 'img1.jpg', url: 'http://www.xxx.com/img1.jpg' }, { name: 'img2.jpg', url: 'http://www.xxx.com/img2.jpg' } ] // 默认已上传的文件列表 }, validate:[] }
accept File type: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept
Related recommendations:
form realizes automatic submission code sharing
JavaScript method example of dynamically adding Form form elements
The above is the detailed content of Dynamically generated form implementation method. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Realizing love animation effects through Java code In the field of programming, animation effects are very common and popular. Various animation effects can be achieved through Java code, one of which is the heart animation effect. This article will introduce how to use Java code to achieve this effect and give specific code examples. The key to realizing the heart animation effect is to draw the heart-shaped pattern and achieve the animation effect by changing the position and color of the heart shape. Here is the code for a simple example: importjavax.swing.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

Implementing exact division operations in Golang is a common need, especially in scenarios involving financial calculations or other scenarios that require high-precision calculations. Golang's built-in division operator "/" is calculated for floating point numbers, and sometimes there is a problem of precision loss. In order to solve this problem, we can use third-party libraries or custom functions to implement exact division operations. A common approach is to use the Rat type from the math/big package, which provides a representation of fractions and can be used to implement exact division operations.

Forms are an integral part of writing a website or application. Laravel, as a popular PHP framework, provides rich and powerful form classes, making form processing easier and more efficient. This article will introduce some tips on using Laravel form classes to help you improve development efficiency. The following explains in detail through specific code examples. Creating a form To create a form in Laravel, you first need to write the corresponding HTML form in the view. When working with forms, you can use Laravel
