Commit 88755a76 by xinla

智能表单分页校验优化

1 parent 7dfb4aea
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
</el-form-item> </el-form-item>
<el-form-item <el-form-item
v-if="activeData.label !== undefined && activeData.formCodeId != 0 && activeData.formCodeId != 1" v-if="activeData.label !== undefined && ![0, 1, 5, 11].includes(+activeData.formCodeId)"
label="标题"> label="标题">
<el-input v-model="activeData.label" placeholder="请输入标题" /> <el-input v-model="activeData.label" placeholder="请输入标题" />
</el-form-item> </el-form-item>
...@@ -725,12 +725,12 @@ export default { ...@@ -725,12 +725,12 @@ export default {
//单选与多选的转换 //单选与多选的转换
switchChange(newX) { switchChange(newX) {
if (newX) { if (newX) {
this.activeData.label = '多选项' // this.activeData.label = '多选项'
this.activeData.tag = 'el-checkbox-group' this.activeData.tag = 'el-checkbox-group'
this.activeData.tagIcon = 'checkbox' this.activeData.tagIcon = 'checkbox'
this.activeData.defaultValue = [] this.activeData.defaultValue = []
} else { } else {
this.activeData.label = '单选项' // this.activeData.label = '单选项'
this.activeData.tag = 'el-radio-group' this.activeData.tag = 'el-radio-group'
this.activeData.tagIcon = 'radio' this.activeData.tagIcon = 'radio'
this.activeData.defaultValue = undefined this.activeData.defaultValue = undefined
......
...@@ -80,7 +80,7 @@ ...@@ -80,7 +80,7 @@
<div @click="dePage" class="formDetailPush">上一页</div> <div @click="dePage" class="formDetailPush">上一页</div>
</div> </div>
<div v-if="page == pageData.length" style="padding: 1rem; position: relative; top: 0.5rem"> <div v-if="page == pageData.length" style="padding: 1rem; position: relative; top: 0.5rem">
<div @click.stop="fromPush()" class="formDetailPush">确认提交</div> <div @click.stop="submit()" class="formDetailPush">确认提交</div>
</div> </div>
<div v-if="page != pageData.length && page == 1" style="padding: 1rem; position: relative; top: 0.5rem"> <div v-if="page != pageData.length && page == 1" style="padding: 1rem; position: relative; top: 0.5rem">
<div @click="addPage" class="formDetailPush">下一页</div> <div @click="addPage" class="formDetailPush">下一页</div>
...@@ -166,7 +166,7 @@ ...@@ -166,7 +166,7 @@
<div @click="dePage" class="formDetailPush">上一页</div> <div @click="dePage" class="formDetailPush">上一页</div>
</div> </div>
<div v-if="page == pageData.length && !ifEnd" style="padding: 1rem; position: relative; top: 0.5rem"> <div v-if="page == pageData.length && !ifEnd" style="padding: 1rem; position: relative; top: 0.5rem">
<div @click.stop="fromPush()" class="formDetailPush">确认提交</div> <div @click.stop="submit()" class="formDetailPush">确认提交</div>
</div> </div>
<div v-if="page == pageData.length && ifEnd" style="padding: 1rem; position: relative; top: 0.5rem"> <div v-if="page == pageData.length && ifEnd" style="padding: 1rem; position: relative; top: 0.5rem">
<div class="formDetailPush">您已完成填写</div> <div class="formDetailPush">您已完成填写</div>
...@@ -416,13 +416,6 @@ export default { ...@@ -416,13 +416,6 @@ export default {
back() { back() {
this.$router.go(-1) //返回上一层 this.$router.go(-1) //返回上一层
}, },
submitForm() {
let that = this
this.$refs['elForm'].validate((valid) => {
if (!valid) return
that.fromPush()
})
},
//开始填写 //开始填写
startFillIn() { startFillIn() {
if (this.style != 'y' || this.index != 1) { if (this.style != 'y' || this.index != 1) {
...@@ -445,23 +438,78 @@ export default { ...@@ -445,23 +438,78 @@ export default {
// this.$forceUpdate() // this.$forceUpdate()
} }
}, },
//表单验证
validate(page) {
// this.$refs['elForm'].validate((valid) => {
// if (!valid) return
// })
let pageData = this.pageData
page = page || pageData.length
for (let i = page ? page - 1 : 0; i < page; i++) {
for (let q = 0; q < pageData[i].length; q++) {
let curItem = JSON.parse(JSON.stringify(pageData[i][q]))
if (curItem.required && ![0, 1].includes(+curItem.formCodeId)) {
let label = curItem.label.substring(curItem.label.indexOf('.') + 1)
if (
curItem.defaultValue === '' ||
curItem.defaultValue === undefined ||
curItem.defaultValue.length === 0
) {
// this.toast('第' + curItem.page + '页,' + label + ',' + '不能为空')
this.toast(`第${curItem.page}页,${label}不能为空`)
return
}
if (curItem.formCodeId == 4) {
if (curItem.max) {
if (curItem.defaultValue > curItem.max) {
this.toast(`第${curItem.page}页,${label},数字文本中数字不能大于${curItem.max}`)
return
}
}
if (curItem.min) {
if (curItem.defaultValue < curItem.min) {
this.toast(`第${curItem.page}页,${label},数字文本中数字不能小于${curItem.min}`)
return
}
}
}
let component = inputComponents.find((item) => item.formCodeId === curItem.formCodeId)
if (Array.isArray(component.regList) && component.regList.length != 0) {
let isRegValidate = component.regList.every((item) => {
if (!curItem.defaultValue.match(item.pattern)) {
this.toast(`第${curItem.page}页,${label},${item.message}`)
return false
} else {
return true
}
})
if (!isRegValidate) return
}
}
}
}
return true
},
//表单提交 //表单提交
async fromPush() { async submit() {
if (!this.validate()) {
return
}
try { try {
this.userIp = this.userIp || (await getIP()) this.userIp = this.userIp || (await getIP())
} catch (error) { } catch (error) {
this.toast('获取ip失败') this.toast('获取ip失败')
return return
} }
let that = this
let answer = [] let answer = []
let pageDatess = that.pageData let pageData = this.pageData
//饼图参数 let btData = [] //饼图参数
let btData = [] for (let i = 0; i < pageData.length; i++) {
for (let i = 0; i < pageDatess.length; i++) { for (let q = 0; q < pageData[i].length; q++) {
for (let q = 0; q < pageDatess[i].length; q++) { let curItem = pageData[i][q]
let curItem = pageDatess[i][q]
if (![0, 1].includes(+curItem.formCodeId)) { if (![0, 1].includes(+curItem.formCodeId)) {
let cloneCurItem = JSON.parse(JSON.stringify(curItem)) let cloneCurItem = JSON.parse(JSON.stringify(curItem))
cloneCurItem.questionNumber = answer.length cloneCurItem.questionNumber = answer.length
...@@ -500,6 +548,8 @@ export default { ...@@ -500,6 +548,8 @@ export default {
// } // }
cloneCurItem.options = cloneCurItem.options.map((e) => e.label).join(',') cloneCurItem.options = cloneCurItem.options.map((e) => e.label).join(',')
} }
} else if (curItem.formCodeId == 10) {
curItem.timeS = this.formateDate(i, curItem.defaultValue, curItem)
} }
if (cloneCurItem.formCodeId == 8 && Array.isArray(cloneCurItem.defaultValue)) { if (cloneCurItem.formCodeId == 8 && Array.isArray(cloneCurItem.defaultValue)) {
if (cloneCurItem.defaultValue.length === 1) { if (cloneCurItem.defaultValue.length === 1) {
...@@ -510,7 +560,7 @@ export default { ...@@ -510,7 +560,7 @@ export default {
if (q == 0) { if (q == 0) {
cloneCurItem.defaultValue = defaultValue[q] + '' cloneCurItem.defaultValue = defaultValue[q] + ''
} else { } else {
let _cloneCurItem = that.deepCopy(cloneCurItem) let _cloneCurItem = this.deepCopy(cloneCurItem)
_cloneCurItem.defaultValue = defaultValue[q] + '' _cloneCurItem.defaultValue = defaultValue[q] + ''
answer.push(_cloneCurItem) answer.push(_cloneCurItem)
btData.push(_cloneCurItem) btData.push(_cloneCurItem)
...@@ -521,59 +571,21 @@ export default { ...@@ -521,59 +571,21 @@ export default {
} }
} }
} }
console.log('answer', answer)
for (let i = 0; i < answer.length; i++) {
let curItem = answer[i]
if (curItem.required) {
let label = curItem.label.substring(curItem.label.indexOf('.') + 1)
if (curItem.defaultValue === '' || curItem.defaultValue === undefined || curItem.defaultValue.length === 0) {
// this.toast('第' + curItem.page + '页,' + label + ',' + '不能为空')
this.toast(`第${curItem.page}页,${label}不能为空`)
return
}
if (curItem.formCodeId == 4) { let clannelsName = this.fromList.channelsName.split(',')
if (curItem.max) { let clannelsPath = this.fromList.channelsPath.split(',')
if (curItem.defaultValue > curItem.max) {
this.toast('第' + curItem.page + '页,' + label + ',数字文本中数字不能大于' + curItem.max)
return
}
}
if (curItem.min) {
if (curItem.defaultValue < curItem.min) {
this.toast('第' + curItem.page + '页,' + label + ',数字文本中数字不能小于' + curItem.min)
return
}
}
} else if (curItem.formCodeId == 10) {
curItem.timeS = this.formateDate(i, curItem.defaultValue, curItem)
}
if (curItem.regList && curItem.regList.length != 0) {
curItem.regList[0].pattern = curItem.regList[0].pattern.substring(1, curItem.regList[0].pattern.length - 2)
let a = new RegExp(curItem.regList[0].pattern)
if (!a.test(curItem.defaultValue)) {
this.toast('第' + curItem.page + '页,' + label + ',' + curItem.regList[0].message)
return
}
}
}
}
let clannelsName = that.fromList.channelsName.split(',')
let clannelsPath = that.fromList.channelsPath.split(',')
for (let i = 0; i < clannelsName.length; i++) { for (let i = 0; i < clannelsName.length; i++) {
if (clannelsPath[i] == that.dataSource) { if (clannelsPath[i] == this.dataSource) {
that.dataSource = clannelsName[i] this.dataSource = clannelsName[i]
break break
} }
if (that.clannelsName == that.dataSource && clannelsName[i] != '默认渠道') { if (this.clannelsName == this.dataSource && clannelsName[i] != '默认渠道') {
break break
} }
} }
let data = { let data = {
mobile: that.formData.mobile, mobile: this.formData.mobile,
name: that.formData.userName, name: this.formData.userName,
//开始填写时间 //开始填写时间
anTime: this.anTime, anTime: this.anTime,
//填写用时 //填写用时
...@@ -602,9 +614,9 @@ export default { ...@@ -602,9 +614,9 @@ export default {
submitForm(data) submitForm(data)
.then((response) => { .then((response) => {
if (response.code == 200) { if (response.code == 200) {
that.ifEnd = true this.ifEnd = true
if (btData.length != 0) { if (btData.length != 0) {
that.insertPieValue(btData) this.insertPieValue(btData)
} }
} else { } else {
this.toast(response.msg) this.toast(response.msg)
...@@ -860,9 +872,11 @@ export default { ...@@ -860,9 +872,11 @@ export default {
}, },
//下一页 //下一页
addPage() { addPage() {
this.page++ if (this.validate(this.page)) {
this.drawingList = this.pageData[this.page - 1] this.page++
that.$forceUpdate() this.drawingList = this.pageData[this.page - 1]
// that.$forceUpdate()
}
}, },
toast(message) { toast(message) {
showFailToast({ showFailToast({
......
...@@ -17,6 +17,65 @@ export const formConf = { ...@@ -17,6 +17,65 @@ export const formConf = {
*/ */
export const inputComponents = [ export const inputComponents = [
{ {
formCodeId: 11,
label: '姓名',
//文字说明
captions: '',
tag: 'el-input',
tagIcon: 'input',
layout: 'colFormItem',
placeholder: '请输入你的名字',
defaultValue: undefined,
span: 24,
labelWidth: null,
style: { width: '100%' },
clearable: true,
prepend: '',
append: '',
'prefix-icon': '',
'suffix-icon': '',
maxlength: null,
'show-word-limit': true,
readonly: false,
disabled: false,
required: true,
regList: [],
changeTag: true,
},
{
formCodeId: 5,
layout: 'colFormItem',
tagIcon: 'phone',
label: '手机号',
//文字说明
captions: '',
formId: 6,
tag: 'el-input',
placeholder: '请输入手机号',
defaultValue: '',
span: 24,
style: { width: '100%' },
clearable: true,
prepend: '',
append: '',
'prefix-icon': '',
'suffix-icon': '',
maxlength: 11,
'show-word-limit': true,
change: 'change',
readonly: false,
disabled: false,
required: true,
changeTag: true,
regList: [
{
pattern: /^1[^012]\d{9}$/, // 匹配的字符串或正则表达式 传递给string.match()校验
message: '手机号格式错误',
},
],
},
{
formCodeId: 0, formCodeId: 0,
label: '文字描述', label: '文字描述',
tag: 'el-input', tag: 'el-input',
...@@ -138,38 +197,6 @@ export const inputComponents = [ ...@@ -138,38 +197,6 @@ export const inputComponents = [
}, },
{ {
formCodeId: 5,
layout: 'colFormItem',
tagIcon: 'phone',
label: '手机号',
//文字说明
captions: '',
formId: 6,
tag: 'el-input',
placeholder: '请输入手机号',
defaultValue: '',
span: 24,
style: { width: '100%' },
clearable: true,
prepend: '',
append: '',
'prefix-icon': '',
'suffix-icon': '',
maxlength: 11,
'show-word-limit': true,
change: 'change',
readonly: false,
disabled: false,
required: true,
changeTag: true,
regList: [
{
pattern: '/^1(3|4|5|7|8|9)\\d{9}$/',
message: '手机号格式错误',
},
],
},
{
formCodeId: 6, formCodeId: 6,
label: '下拉选择', label: '下拉选择',
tag: 'el-select', tag: 'el-select',
...@@ -223,7 +250,7 @@ export const inputComponents = [ ...@@ -223,7 +250,7 @@ export const inputComponents = [
}, },
{ {
formCodeId: 8, formCodeId: 8,
label: '单选项', label: '选项组',
tag: 'el-radio-group', tag: 'el-radio-group',
_props: { _props: {
tag: 'div', tag: 'div',
...@@ -294,32 +321,6 @@ export const inputComponents = [ ...@@ -294,32 +321,6 @@ export const inputComponents = [
format: 'YYYY-MM-DD HH:mm:ss', format: 'YYYY-MM-DD HH:mm:ss',
changeTag: true, changeTag: true,
}, },
{
formCodeId: 11,
label: '姓名',
//文字说明
captions: '',
tag: 'el-input',
tagIcon: 'input',
layout: 'colFormItem',
placeholder: '请输入你的名字',
defaultValue: undefined,
span: 24,
labelWidth: null,
style: { width: '100%' },
clearable: true,
prepend: '',
append: '',
'prefix-icon': '',
'suffix-icon': '',
maxlength: null,
'show-word-limit': true,
readonly: false,
disabled: false,
required: true,
regList: [],
changeTag: true,
},
] ]
// export const selectComponents = [ // export const selectComponents = [
......
...@@ -53,7 +53,7 @@ export default [ ...@@ -53,7 +53,7 @@ export default [
changeTag: true, changeTag: true,
regList: [ regList: [
{ {
pattern: '/^1(3|4|5|7|8|9)\\d{9}$/', pattern: /^1[^012]\d{9}$/, // 匹配的字符串或正则表达式 传递给string.match()校验
message: '手机号格式错误', message: '手机号格式错误',
}, },
], ],
......
...@@ -24,7 +24,7 @@ function vModel(self, dataObject, defaultValue) { ...@@ -24,7 +24,7 @@ function vModel(self, dataObject, defaultValue) {
// console.log('self', self) // console.log('self', self)
if (dataObject.formCodeId == 5 || dataObject.formCodeId == 625) { if (dataObject.formCodeId == 5 || dataObject.formCodeId == 625) {
if (defaultValue) { if (defaultValue) {
console.log('(/(^[1-9]d*$)/.test(defaultValue))', /(^[1-9]\d*$)/.test(defaultValue)) // console.log('(/(^[1-9]d*$)/.test(defaultValue))', /(^[1-9]\d*$)/.test(defaultValue))
if (/(^[1-9]\d*$)/.test(defaultValue)) { if (/(^[1-9]\d*$)/.test(defaultValue)) {
} else { } else {
defaultValue = defaultValue.replace(/[^\d]/g, '') defaultValue = defaultValue.replace(/[^\d]/g, '')
......
...@@ -287,7 +287,7 @@ export default { ...@@ -287,7 +287,7 @@ export default {
// changeTag: true, // changeTag: true,
// regList: [ // regList: [
// { // {
// pattern: '/^1(3|4|5|7|8|9)\\d{9}$/', // pattern: /^1(3|4|5|7|8|9)\d{9}$/,
// message: '手机号格式错误', // message: '手机号格式错误',
// }, // },
// ], // ],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!