# weui_form_demo **Repository Path**: fanhangcheng/weui_form_demo ## Basic Information - **Project Name**: weui_form_demo - **Description**: 封装基于weui的form组件 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2022-06-18 - **Last Updated**: 2023-04-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README
# 封装WeUI Form组件 ## 参数说明
属性描述默认值必需
formData表单绑定数据,用于初始化{}
options表单配置[]
rules表单验证规则[]
title卡片标题“”
## options
属性描述必需
title表单表头
key表单对应的字段
type控件类型
rules是否开启验证
option表单配置
disabled禁用
slot开启自定义控件

如果开启了 slot ,一定要指定好 slot="需要自定义的字段"

## type
属性描述
selector选择框
text文本输入框
textarea文本域
number数字
idcard身份证
digit小数点
password密码
switch开关
date日期
time时间
region地区选择
file图片上传
## option

只有在type[selector]时才需要设置

属性描述必需
data数据源
props配置
## props

如果是普通选择框无需设置

属性描述必需
Key指定对象选择框的显示字段
## 方法
属性描述回调函数
validate()表单验证isValidate
## 示例代码 ### index.json ```json { "navigationBarBackgroundColor": "#3b4994", "navigationBarTextStyle": "white", "navigationBarTitleText": "测试页面", "usingComponents": { "my-form": "/components/Form/sform", "mp-icon": "weui-miniprogram/icon/icon" } } ``` ### index.wxml ```html 其他备注: ``` ### index.js ```js Page({ /** * 页面的初始数据 */ data: { myFormData: { formData: { 1: "", 2: "", 3: "", 4: "", 5: "", test_slot: "", test_area:"" }, options: [{ title: "调查总穴数", key: "1", type: "selector", rules: true, option: { data: [{ id: 1, name: "选项1" }, { id: 2, name: "选项2" }, { id: 3, name: "选项3" }, { id: 4, name: "选项4" } ], props: { key: "name" } } }, { title: "调查总株数", key: "2", type: "selector", rules: true, option: { data: ["选项1", "选项2", "选项3"] } }, { title: "调查总穗数", key: "3", type: "text", rules: true }, { title: "活虫数", key: "4", type: "textarea", rules: true }, { title: "数字", key: "5", type: "number", rules: true }, { title:"", key: "test_slot", slot: true }, { title:"", key: "test_area", slot: true } ], rules: [] }, }, // 提交表单 submitForm() { // 获取组件对象 const myForm = this.selectComponent("#my-form"); // 调用验证方法 if (myForm.validate()) { console.log(myForm.data.formData); } } }) ``` ### index.wxss ```css .main { padding: 5vh 0; } #my-form input { border:1rpx solid red; padding-left: 10rpx } .slot-box { display: flex; justify-content: space-between; align-items: center; } .inp1 { width: 400rpx; font-size: 14px; border-radius: 20rpx; border-color:#bbb !important; } .inp2 { width: 150rpx; } .slot-box2 { margin-top: 50rpx; display: flex; justify-content: space-between; } .slot-box2 text { width: 300rpx; } .slot-box2 textarea { width: 100%; border:1px solid #bbb } ```