新聞中心
這里有您想知道的互聯網營銷解決方案
創(chuàng)新互聯小程序教程:微信小程序WeUI·Form
Form
Form表單組件,結合Cell、Checkbox-group、Checkbox組件等做表單校驗。

在平遠等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供做網站、成都網站設計 網站設計制作按需網站建設,公司網站建設,企業(yè)網站建設,品牌網站設計,營銷型網站,成都外貿網站建設公司,平遠網站建設費用合理。
示例代碼:
{
"component": true,
"usingComponents": {
"mp-toptips": "../components/toptips/toptips",
"mp-cells": "../components/cells/cells",
"mp-cell": "../components/cell/cell",
"mp-checkbox": "../components/checkbox/checkbox",
"mp-checkbox-group": "../components/checkbox-group/checkbox-group",
"mp-form": "../components/form/form"
}
}
Form
表單輸入
獲取驗證碼
{{date}}
開關
標題文字
文本框
文本域
0/200
選擇
{{countryCodes[countryCodeIndex]}}
選擇
{{accounts[accountIndex]}}
國家/地區(qū)
{{countries[countryIndex]}}
Component({
data: {
showTopTips: false,
radioItems: [
{name: 'cell standard', value: '0', checked: true},
{name: 'cell standard', value: '1'}
],
checkboxItems: [
{name: 'standard is dealt for u.', value: '0', checked: true},
{name: 'standard is dealicient for u.', value: '1'}
],
items: [
{name: 'USA', value: '美國'},
{name: 'CHN', value: '中國', checked: 'true'},
{name: 'BRA', value: '巴西'},
{name: 'JPN', value: '日本'},
{name: 'ENG', value: '英國'},
{name: 'TUR', value: '法國'},
],
date: "2016-09-01",
time: "12:01",
countryCodes: ["+86", "+80", "+84", "+87"],
countryCodeIndex: 0,
countries: ["中國", "美國", "英國"],
countryIndex: 0,
accounts: ["微信號", "QQ", "Email"],
accountIndex: 0,
isAgree: false,
formData: {
},
rules: [{
name: 'radio',
rules: {required: true, message: '單選列表是必選項'},
}, {
name: 'checkbox',
rules: {required: true, message: '多選列表是必選項'},
}, {
name: 'qq',
rules: {required: true, message: 'qq必填'},
}, {
name: 'mobile',
rules: [{required: true, message: 'mobile必填'}, {mobile: true, message: 'mobile格式不對'}],
}, {
name: 'vcode',
rules: {required: true, message: '驗證碼必填'},
}, {
name: 'idcard',
rules: {required: true, message: 'idcard必填'},
}]
},
methods: {
radioChange: function (e) {
console.log('radio發(fā)生change事件,攜帶value值為:', e.detail.value);
var radioItems = this.data.radioItems;
for (var i = 0, len = radioItems.length; i < len; ++i) {
radioItems[i].checked = radioItems[i].value == e.detail.value;
}
this.setData({
radioItems: radioItems,
[`formData.radio`]: e.detail.value
});
},
checkboxChange: function (e) {
console.log('checkbox發(fā)生change事件,攜帶value值為:', e.detail.value);
var checkboxItems = this.data.checkboxItems, values = e.detail.value;
for (var i = 0, lenI = checkboxItems.length; i < lenI; ++i) {
checkboxItems[i].checked = false;
for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
if(checkboxItems[i].value == values[j]){
checkboxItems[i].checked = true;
break;
}
}
}
this.setData({
checkboxItems: checkboxItems,
[`formData.checkbox`]: e.detail.value
});
},
bindDateChange: function (e) {
this.setData({
date: e.detail.value,
[`formData.date`]: e.detail.value
})
},
formInputChange(e) {
const {field} = e.currentTarget.dataset
this.setData({
[`formData.${field}`]: e.detail.value
})
},
bindTimeChange: function (e) {
this.setData({
time: e.detail.value
})
},
bindCountryCodeChange: function(e){
console.log('picker country code 發(fā)生選擇改變,攜帶值為', e.detail.value);
this.setData({
countryCodeIndex: e.detail.value
})
},
bindCountryChange: function(e) {
console.log('picker country 發(fā)生選擇改變,攜帶值為', e.detail.value);
this.setData({
countryIndex: e.detail.value
})
},
bindAccountChange: function(e) {
console.log('picker account 發(fā)生選擇改變,攜帶值為', e.detail.value);
this.setData({
accountIndex: e.detail.value
})
},
bindAgreeChange: function (e) {
this.setData({
isAgree: !!e.detail.value.length
});
},
submitForm() {
this.selectComponent('#form').validate((valid, errors) => {
console.log('valid', valid, errors)
if (!valid) {
const firstError = Object.keys(errors)
if (firstError.length) {
this.setData({
error: errors[firstError[0]].message
})
}
} else {
wx.showToast({
title: '校驗通過'
})
}
})
}
}
});
屬性列表
| 屬性 | 類型 | 默認值 | 必填 | 說明 |
|---|---|---|---|---|
| ext-class | string | 否 | 添加在組件內部結構的class,可用于修改組件內部的樣式 | |
| rules | object |
否 | 表單校驗的規(guī)則列表,格式下面詳細介紹 | |
| models | object | 否 | 需要校驗的表單的數據 | |
| bindsuccess | eventhandler | 否 | 校驗成功觸發(fā)的事件,detail是{trigger},trigger的值是change或validate,表示是輸入改成觸發(fā)的校驗還是主動調用的validate接口 | |
| bindfail | eventhandler | 否 | 校驗失敗觸發(fā)的事件,detail是{trigger, errors},trigger的值是change或validate,表示是輸入改成觸發(fā)的校驗還是主動調用的validate接口。errors是錯誤的字段列表。 |
rules是表單校驗的規(guī)則列表,列表每一項表示一個字段的校驗規(guī)則,注意,必須要在Cell或Checkbox-group組件聲明prop屬性,表單校驗規(guī)則才生效,表單校驗規(guī)則的定義如下:
| 屬性 | 類型 | 默認值 | 必填 | 說明 |
|---|---|---|---|---|
| name | string | 是 | 校驗的字段名 | |
| rules | array/object | 是 | 校驗的規(guī)則,如果有多項,則是數組 | |
| rules.message | string | 否 | 校驗失敗時候提示的文字 | |
| rules.validator | function | 否 | 自定義校驗函數,接受rule, value, param, models四個參數,其中rule格式為{name: '字段名', message: '失敗信息'}, value是字段值,param是校驗參數,models是form組件的models屬性。函數返回錯誤提示,表示校驗失敗,錯誤提示會通過回調返回給開發(fā)者 | |
| rules.[rule] | string | 否 | rule是變量,表示內置的校驗規(guī)則名稱,比如required,則校驗規(guī)則對象為{name: "fieldName", rules: {required: true}},下面會詳細介紹所有的內置規(guī)則 |
內置校驗規(guī)則
| 規(guī)則名 | 參數 | 說明 |
|---|---|---|
| required | 是否必填 | |
| minlength | number | 最小長度 |
| maxlength | number | 最大長度 |
| rangelength | [number, number] | 長度范圍,參數為[最小長度, 最大長度] |
| bytelength | number | 字節(jié)長度 |
| range | [number, number] | 數字的大小范圍 |
| min | number | 最小值限制 |
| max | number | 最大值限制 |
| mobile | 手機號碼校驗 | |
| 電子郵件校驗 | ||
| url | URL鏈接地址校驗 | |
| equalTo | string | 相等校驗,參數是另外一個字段名 |
接口
validate
validate接口接受類型為function的callback,callback有isValid和errors兩個參數,isValid表示是否校驗通過,errors在校驗失敗的時候的值為失敗的字段列表。
validateField
validateField接口接受2個參數, 第一個是字段名,第二個是類型為function的callback,callback有isValid和errors兩個參數,isValid表示是否校驗通過,errors在校驗失敗的時候的值為失敗的字段列表。
Slot
| 名稱 | 描述 |
|---|---|
| 默認 | 內容slot |
本文名稱:創(chuàng)新互聯小程序教程:微信小程序WeUI·Form
地址分享:http://fisionsoft.com.cn/article/dhgopss.html


咨詢
建站咨詢
