Commit 8275aa4b by chenwq

表单校验

1 parent 4185d2a1
const phoneRex = /^[1]([3-9])[0-9]{9}$/
const formRule = {
merchantName: [{ required: true, message: '服务中心名称不能为空' }],
contactName: [{ required: true, message: '联系人不能为空' }],
phone: [
{ required: true, message: '联系电话不能为空' },
{
validator: (rule, value) => {
return !phoneRex.test(value) ? { message: '请输入正确的手机号' } : null
}
}
],
password: [
{ required: true, message: '登录密码不能为空' },
{
validator: (rule, value) => {
return /^[a-zA-Z0-9]{6,20}$/.test(value) ? null : { message: '登陆密码请输入6-20位数字和字母组合' }
}
}
],
}
const formItems = [{
label: '服务中心名称',
props: 'merchantName',
type: 'input',
maxlength: 50
}, {
label: '联系人',
props: 'contactName',
type: 'input',
placeholder: '请输入联系人姓名',
maxlength: 10
}, {
label: '联系电话',
tips: '(登录凭证)',
props: 'phone',
type: 'input',
placeholder: '请输入手机号码',
maxlength: 11
}, {
label: '登录密码',
tips: '(可用于PC端登录)',
props: 'password',
type: 'input',
placeholder: '请输入登录密码(6-20位字母+数字)',
maxlength: 20
}]
export default {
formRule,
formItems
}
\ No newline at end of file
// packageMine/pages/serviceManage/editService/editService.js
import constantData from './constant/data'
Page({
/**
......@@ -7,25 +8,24 @@ Page({
data: {
addOrEdit: '',
formData: {},
formItems: [{
label: '服务中心名称',
props: 'merchantName',
type: 'input'
}, {
label: '联系人',
props: 'contactName',
type: 'input'
}, {
label: '联系电话',
tips: '(登录凭证)',
props: 'phone',
type: 'input'
}, {
label: '登录密码',
tips: '(可用于PC端登录)',
props: 'password',
type: 'input'
}],
formItems: constantData.formItems,
},
handleInput(e) {
let { attr } = e.currentTarget.dataset
let { value } = e.detail
this.setData({
['formData.' + attr]: value
})
},
handleConfirm() {
let { addOrEdit, formData } = this.data
let validObj = wx.$util.checkRule(formData, constantData.formRule)
if (validObj) {
wx.$api.showToast(validObj.message)
return
}
},
/**
......
......@@ -8,7 +8,7 @@
<block wx:for="{{formItems}}" wx:key="index">
<view class="label">{{item.label}}<text class="tips">{{item.tips}}</text></view>
<view class="input-box" wx:if="{{item.type == 'input'}}">
<input type="text" placeholder="{{item.placeholder || '请输入' + item.label}}" class="input" value="{{formData[item.props]}}" placeholder-class="input-placeholder" />
<input type="text" placeholder="{{item.placeholder || '请输入' + item.label}}" maxlength="{{item.maxlength}}" class="input" value="{{formData[item.props]}}" placeholder-class="input-placeholder" data-attr="{{item.props}}" bindinput="handleInput" />
</view>
<view class="select-box" wx:if="{{item.type == 'select'}}" bindtap="jump" data-type="{{item.pageType}}">
<view class="name" wx:if="{{formData[item.valueFn]}}">{{formData[item.valueFn]}}</view>
......@@ -20,5 +20,4 @@
<view class="isFullSucreen btn">
<view class="btn-blue" bindtap="handleConfirm">{{addOrEdit == 'add' ? '提交' : '保存'}}</view>
</view>
</view>
<select-service-confirm show="{{showServiceConfirm}}" tabBarStyle="{{tabBarStyle}}" serviceList="{{serviceList}}" bind:hiddenSelectConfirm="hiddenSelectConfirm"></select-service-confirm>
\ No newline at end of file
</view>
\ No newline at end of file
......@@ -288,6 +288,48 @@ const moneyThreeForTwoThousands = num => {
return d + (result + num2)
}
/**
* 动态校验字段
* @param {*} obj 需校验的对象
* @param {*} formRule 校验规则
* formRule = {
* id: [
* { required: true, message: 'xx不能为空' },
* {
* validator: (rule, value) => {
* return value != 'cwq' ? { message: '错误信息' } : null
* }
* }
* ],
* }
*/
const checkRule = function (obj, formRule) {
let target = Object.keys(formRule)
for (let i = 0; i < target.length; i++) {
let attr = target[i]
let item = formRule[attr]
let requirObj = item.find(k => k.required == true)
let validFn = item.find(k => k.validator)
if (requirObj && !obj[attr]) {
return {
...requirObj,
attr
}
}
if (validFn) {
let validObj = validFn.validator({}, obj[attr], obj)
if (validObj) {
return {
...validObj,
attr
}
}
}
}
return false
}
const Util = {
getImgVersion,
......@@ -300,6 +342,7 @@ const Util = {
getCurrentPageUrl,
deepClone,
setNavigationBarTitle,
moneyThreeForTwoThousands
moneyThreeForTwoThousands,
checkRule
};
export default Util;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!