Commit 161001ea authored by 罗林杰's avatar 罗林杰

修改表单校验

parent f0fbca24
...@@ -9,21 +9,24 @@ ...@@ -9,21 +9,24 @@
labelPosition="left" labelPosition="left"
:model="userInfo" :model="userInfo"
labelWidth="90" labelWidth="90"
ref="uForm"
:rules="rules"
> >
<u-form-item <u-form-item
label="手机号" label="手机号"
prop="userInfo.memPhone" prop="memPhone"
borderBottom borderBottom
> >
<u--input <u--input
v-model="userInfo.memPhone" v-model="userInfo.memPhone"
border="none" border="none"
placeholder="请输入手机号" placeholder="请输入手机号"
maxlength="11"
></u--input> ></u--input>
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="微信号" label="微信号"
prop="userInfo.memWxCode" prop="memWxCode"
borderBottom borderBottom
> >
<u--input <u--input
...@@ -34,7 +37,7 @@ ...@@ -34,7 +37,7 @@
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="民族" label="民族"
prop="userInfo.memNationText" prop="form.memNationText"
borderBottom borderBottom
@click="showNation = true; hideKeyboard()" @click="showNation = true; hideKeyboard()"
> >
...@@ -52,7 +55,7 @@ ...@@ -52,7 +55,7 @@
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="昵称" label="昵称"
prop="userInfo.memNickName" prop="memNickName"
borderBottom borderBottom
> >
<u--input <u--input
...@@ -63,7 +66,7 @@ ...@@ -63,7 +66,7 @@
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="性别" label="性别"
prop="userInfo.memSex" prop="form.memSexText"
borderBottom borderBottom
@click="showSex = true; hideKeyboard()" @click="showSex = true; hideKeyboard()"
> >
...@@ -81,7 +84,7 @@ ...@@ -81,7 +84,7 @@
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="生日" label="生日"
prop="userInfo.memBirthday" prop="memBirthday"
borderBottom borderBottom
@click="showBirthday = true; hideKeyboard()" @click="showBirthday = true; hideKeyboard()"
> >
...@@ -99,7 +102,7 @@ ...@@ -99,7 +102,7 @@
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="星座" label="星座"
prop="userInfo.memConstellation" prop="form.memConstellationText"
borderBottom borderBottom
> >
<u--input <u--input
...@@ -112,7 +115,7 @@ ...@@ -112,7 +115,7 @@
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="身高" label="身高"
prop="userInfo.memHeight" prop="memHeight"
borderBottom borderBottom
> >
<u--input <u--input
...@@ -120,12 +123,15 @@ ...@@ -120,12 +123,15 @@
disabledColor="#ffffff" disabledColor="#ffffff"
placeholder="请输入身高" placeholder="请输入身高"
border="none" border="none"
type="digit"
maxlength="3"
@input="limitHeightNum"
></u--input> ></u--input>
<span slot="right">cm</span> <span slot="right">cm</span>
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="体重" label="体重"
prop="userInfo.memWeight" prop="memWeight"
borderBottom borderBottom
> >
<u--input <u--input
...@@ -133,12 +139,15 @@ ...@@ -133,12 +139,15 @@
disabledColor="#ffffff" disabledColor="#ffffff"
placeholder="请输入体重" placeholder="请输入体重"
border="none" border="none"
type="digit"
maxlength="3"
@input="limitWeightNum"
></u--input> ></u--input>
<span slot="right">kg</span> <span slot="right">kg</span>
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="教育经历" label="教育经历"
prop="userInfo.memMaxEducation" prop="form.memMaxEducationText"
borderBottom borderBottom
@click="showEducation = true; hideKeyboard()" @click="showEducation = true; hideKeyboard()"
> >
...@@ -156,7 +165,7 @@ ...@@ -156,7 +165,7 @@
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="毕业院校" label="毕业院校"
prop="userInfo.memCollege" prop="memCollege"
borderBottom borderBottom
> >
<u--input <u--input
...@@ -167,7 +176,7 @@ ...@@ -167,7 +176,7 @@
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="职业" label="职业"
prop="userInfo.memCareer" prop="memCareer"
borderBottom borderBottom
> >
<u--input <u--input
...@@ -178,7 +187,7 @@ ...@@ -178,7 +187,7 @@
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="年收入" label="年收入"
prop="userInfo.memYearIncome" prop="memYearIncome"
borderBottom borderBottom
> >
<u--input <u--input
...@@ -190,7 +199,7 @@ ...@@ -190,7 +199,7 @@
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="工作单位" label="工作单位"
prop="userInfo.memWorkPlace" prop="memWorkPlace"
borderBottom borderBottom
> >
<u--input <u--input
...@@ -201,7 +210,7 @@ ...@@ -201,7 +210,7 @@
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="居住地" label="居住地"
prop="userInfo.memResidenceProvince" prop="form.memResidence"
borderBottom borderBottom
@click="showResidence = true; hideKeyboard()" @click="showResidence = true; hideKeyboard()"
> >
...@@ -219,7 +228,7 @@ ...@@ -219,7 +228,7 @@
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="籍贯" label="籍贯"
prop="userInfo.memResidenceCity" prop="form.memNative"
borderBottom borderBottom
@click="showNative = true; hideKeyboard()" @click="showNative = true; hideKeyboard()"
> >
...@@ -237,40 +246,40 @@ ...@@ -237,40 +246,40 @@
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="自我介绍" label="自我介绍"
prop="userInfo.memIntroduce" prop="memIntroduce"
borderBottom borderBottom
> >
<u--textarea v-model="userInfo.memIntroduce" placeholder="请输入自我介绍"></u--textarea> <u--textarea v-model="userInfo.memIntroduce" placeholder="请输入自我介绍"></u--textarea>
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="兴趣爱好" label="兴趣爱好"
prop="userInfo.memHobby" prop="memHobby"
borderBottom borderBottom
> >
<u--textarea v-model="userInfo.memHobby" placeholder="请输入兴趣爱好" ></u--textarea> <u--textarea v-model="userInfo.memHobby" placeholder="请输入兴趣爱好" ></u--textarea>
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="未来规划" label="未来规划"
prop="userInfo.memFuturePlan" prop="memFuturePlan"
borderBottom borderBottom
> >
<u--textarea v-model="userInfo.memFuturePlan" placeholder="请输入未来规划" ></u--textarea> <u--textarea v-model="userInfo.memFuturePlan" placeholder="请输入未来规划" ></u--textarea>
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="期待" label="期待"
prop="userInfo.memHalfDesire" prop="memHalfDesire"
borderBottom borderBottom
> >
<u--textarea v-model="userInfo.memHalfDesire" placeholder="请输入期待" ></u--textarea> <u--textarea v-model="userInfo.memHalfDesire" placeholder="请输入期待" ></u--textarea>
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="婚姻状况" label="婚姻状况"
prop="userInfo.memMarriage" prop="form.memMarriageText"
borderBottom borderBottom
@click="showMarriage = true; hideKeyboard()" @click="showMarriage = true; hideKeyboard()"
> >
<u--input <u--input
v-model="userInfo.memMarriage" v-model="form.memMarriageText"
disabled disabled
disabledColor="#ffffff" disabledColor="#ffffff"
placeholder="请选择婚姻状况" placeholder="请选择婚姻状况"
...@@ -283,7 +292,7 @@ ...@@ -283,7 +292,7 @@
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="婚姻状况备注" label="婚姻状况备注"
prop="userInfo.memMarriageRemarks" prop="memMarriageRemarks"
borderBottom borderBottom
> >
<u--input <u--input
...@@ -294,7 +303,7 @@ ...@@ -294,7 +303,7 @@
</u-form-item> </u-form-item>
<u-form-item <u-form-item
label="16型人格" label="16型人格"
prop="userInfo.memMbti" prop="memMbti"
borderBottom borderBottom
> >
<u--input <u--input
...@@ -316,18 +325,12 @@ ...@@ -316,18 +325,12 @@
customStyle="margin-top: 10px;margin-bottom:50px" customStyle="margin-top: 10px;margin-bottom:50px"
@click="reset" @click="reset"
></u-button> ></u-button>
<u-code
ref="uCode"
@change="codeChange"
seconds="20"
@start="disabled1 = true"
@end="disabled1 = false"
></u-code>
<!-- 生日--> <!-- 生日-->
<u-datetime-picker <u-datetime-picker
:show="showBirthday" :show="showBirthday"
:value="birthday"
mode="date" mode="date"
:minDate="-1420070400000"
:maxDate="birthday"
closeOnClickOverlay closeOnClickOverlay
@confirm="confirmBirthday" @confirm="confirmBirthday"
@cancel="showBirthday=false" @cancel="showBirthday=false"
...@@ -423,20 +426,13 @@ export default { ...@@ -423,20 +426,13 @@ export default {
}] }]
], ],
//选择器 //选择器
showAge: false,
showSex: false, showSex: false,
showHeight: false,
showCalendar: false,
showBirthday: false, showBirthday: false,
showEducation: false, showEducation: false,
showCareer: false,
showMarriage:false, showMarriage:false,
showNation:false, showNation:false,
showResidence: false, showResidence: false,
showNative: false, showNative: false,
disabled1: false,
tips: '',
value: '',
userInfo: { userInfo: {
nickname:'',//昵称 nickname:'',//昵称
memPhone:'',//手机号 memPhone:'',//手机号
...@@ -467,158 +463,220 @@ export default { ...@@ -467,158 +463,220 @@ export default {
form:{}, form:{},
birthday: Number(new Date()), birthday: Number(new Date()),
rules: { rules: {
'userInfo.name': [{ memPhone: [
type: 'string', {
required: true, type: 'number',
message: '请填写姓名', required: true,
trigger: ['blur', 'change'] message: '请输入手机号',
}, { trigger: ['blur']
// 此为同步验证,可以直接返回true或者false,如果是异步验证,稍微不同,见下方说明
validator: (rule, value, callback) => {
// 调用uView自带的js验证规则,详见:https://www.uviewui.com/js/test.html
return uni.$u.test.chinese(value);
}, },
message: "姓名必须为中文", {
// 触发器可以同时用blur和change,二者之间用英文逗号隔开 pattern: /^1[3-9]\d{9}$/,
trigger: ["change", "blur"], message: '请输入正确的手机号',
}], trigger: ['blur']
code: { }
type: 'string', ],
required: true, memWxCode: [
len: 4, {
message: '请填写4位验证码', type: 'string',
trigger: ['blur'] required: true,
}, message: '请输入微信号',
'userInfo.sex': { trigger: ['blur']
type: 'string', }
max: 1, ],
required: true, 'form.memNationText': [
message: '请选择男或女', {
trigger: ['blur', 'change'] type: 'string',
}, required: true,
radiovalue1: { message: '请选择民族',
type: 'string', trigger: ['change']
min: 1, }
max: 2, ],
message: '橙子有毒', memNickName: [
trigger: ['change'] {
}, type: 'string',
checkboxValue1: { required: true,
type: 'array', message: '请输入昵称',
min: 2, trigger: ['blur']
required: true, }
message: '不能太宅,至少选两项', ],
trigger: ['change'] 'form.memSexText': [
}, {
intro: { type: 'string',
type: 'string', required: true,
min: 3, message: '请选择性别',
required: true, trigger: ['change']
message: '不低于3个字', }
trigger: ['change'] ],
}, memBirthday: [
hotel: { {
type: 'string', type: 'string',
min: 2, required: true,
required: true, message: '请选择生日',
message: '请选择住店时间', trigger: ['change']
trigger: ['change'] }
}, ],
'userInfo.birthday': { memHeight: [
type: 'string', {
required: true, type: 'number',
message: '请选择生日', required: true,
trigger: ['change'] message: '请输入身高',
}, trigger: ['blur']
}, }
radiolist1: [{ ],
name: '苹果', memWeight: [
disabled: false {
}, type: 'number',
{ required: true,
name: '香蕉', message: '请输入体重',
disabled: false trigger: ['blur']
}, }
{ ],
name: '毒橙子', 'form.memMaxEducationText': [
disabled: false {
} type: 'string',
], required: true,
checkboxList1: [{ message: '请选择教育经历',
name: '羽毛球', trigger: ['change']
disabled: false }
}, ],
{ memCollege: [
name: '跑步', {
disabled: false type: 'string',
}, required: true,
{ message: '请输入毕业院校',
name: '爬山', trigger: ['blur']
disabled: false }
} ],
] memYearIncome: [
{
type: 'number',
required: true,
message: '请输入年收入',
trigger: ['blur']
}
],
'form.memResidence': [
{
type: 'string',
required: true,
message: '请选择居住地',
trigger: ['change']
}
],
'form.memNative': [
{
type: 'string',
required: true,
message: '请选择籍贯',
trigger: ['change']
}
],
memIntroduce: [
{
type: 'string',
required: true,
message: '请输入自我介绍',
trigger: ['blur']
}
],
memHobby: [
{
type: 'string',
required: true,
message: '请输入兴趣爱好',
trigger: ['blur']
}
],
memFuturePlan: [
{
type: 'string',
required: true,
message: '请输入未来规划',
trigger: ['blur']
}
],
memHalfDesire: [
{
type: 'string',
required: true,
message: '请输入期待',
trigger: ['blur']
}
],
'form.memMarriageText': [
{
type: 'string',
required: true,
message: '请选择婚姻状况',
trigger: ['change']
}
]
}
} }
}, },
onReady() { onReady() {
// 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则 //如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。
// this.$refs.form1.setRules(this.rules) this.$refs.uForm.setRules(this.rules)
},
onLoad() {
this.getHeight()
}, },
methods: { methods: {
confirmMarriage(e){ confirmMarriage(e){
this.showMarriage = false this.showMarriage = false
this.userInfo.memMarriage = e.value this.userInfo.memMarriage = e.value[0].value
this.form.memMarriageText = e.value[0].label
}, },
confirmEducation(e){ confirmEducation(e){
this.showEducation = false this.showEducation = false
this.userInfo.memMaxEducation = e.value this.userInfo.memMaxEducation = e.value[0].value
this.form.memMaxEducationText = e.data this.form.memMaxEducationText = e.value[0].label
}, },
confirmBirthday(e) { confirmBirthday(e) {
this.showBirthday = false this.showBirthday = false
this.userInfo.memBirthday = uni.$u.timeFormat(e.value, 'yyyy-mm-dd') this.userInfo.memBirthday = uni.$u.timeFormat(e.value, 'yyyy-mm-dd')
this.getConstellation(value) console.log('this.userInfo.memBirthday',this.userInfo.memBirthday)
this.getConstellation(this.userInfo.memBirthday)
}, },
getConstellation (value) { getConstellation (value) {
const month = value.getMonth() + 1 // 月份是从0开始的,+1修正 const [year, month, day] = value.split('-').map(Number);
const day = value.getDate() const dateObject = new Date(year, month - 1, day); // 注意:月份需要减1,因为JS中月份是从0开始计算的
const formattedMonth = dateObject.getMonth() + 1; // 获取正确的月份,+1是因为构造Date时减了1
const dayInMonth = dateObject.getDate();
if ((month === 1 && day >= 20) || (month === 2 && day <= 18)) { if ((formattedMonth === 1 && dayInMonth >= 20) || (formattedMonth === 2 && dayInMonth <= 18)) {
this.form.memConstellation = 1 this.userInfo.memConstellation = 1
this.form.memConstellationText = '水瓶座' this.form.memConstellationText = '水瓶座'
} else if ((month === 2 && day >= 19) || (month === 3 && day <= 20)) { } else if ((formattedMonth === 2 && dayInMonth >= 19) || (formattedMonth === 3 && dayInMonth <= 20)) {
this.form.memConstellation = 2 this.form.memConstellation = 2
this.form.memConstellationText = '双鱼座' this.form.memConstellationText = '双鱼座'
} else if ((month === 3 && day >= 21) || (month === 4 && day <= 19)) { } else if ((formattedMonth === 3 && dayInMonth >= 21) || (formattedMonth === 4 && dayInMonth <= 19)) {
this.form.memConstellation = 3 this.form.memConstellation = 3
this.form.memConstellationText = '白羊座' this.form.memConstellationText = '白羊座'
} else if ((month === 4 && day >= 20) || (month === 5 && day <= 20)) { } else if ((formattedMonth === 4 && dayInMonth >= 20) || (formattedMonth === 5 && dayInMonth <= 20)) {
this.form.memConstellation = 4 this.form.memConstellation = 4
this.form.memConstellationText = '金牛座' this.form.memConstellationText = '金牛座'
} else if ((month === 5 && day >= 21) || (month === 6 && day <= 20)) { } else if ((formattedMonth === 5 && dayInMonth >= 21) || (formattedMonth === 6 && dayInMonth <= 20)) {
this.form.memConstellation = 5 this.form.memConstellation = 5
this.form.memConstellationText = '双子座' this.form.memConstellationText = '双子座'
} else if ((month === 6 && day >= 21) || (month === 7 && day <= 22)) { } else if ((formattedMonth === 6 && dayInMonth >= 21) || (formattedMonth === 7 && dayInMonth <= 22)) {
this.form.memConstellation = 6 this.form.memConstellation = 6
this.form.memConstellationText = '巨蟹座' this.form.memConstellationText = '巨蟹座'
} else if ((month === 7 && day >= 23) || (month === 8 && day <= 22)) { } else if ((formattedMonth === 7 && dayInMonth >= 23) || (formattedMonth === 8 && dayInMonth <= 22)) {
this.form.memConstellation = 7 this.form.memConstellation = 7
this.form.memConstellationText = '狮子座' this.form.memConstellationText = '狮子座'
} else if ((month === 8 && day >= 23) || (month === 9 && day <= 22)) { } else if ((formattedMonth === 8 && dayInMonth >= 23) || (formattedMonth === 9 && dayInMonth <= 22)) {
this.form.memConstellation = 8 this.form.memConstellation = 8
this.form.memConstellationText = '处女座' this.form.memConstellationText = '处女座'
} else if ((month === 9 && day >= 23) || (month === 10 && day <= 22)) { } else if ((formattedMonth === 9 && dayInMonth >= 23) || (formattedMonth === 10 && dayInMonth <= 22)) {
this.form.memConstellation = 9 this.form.memConstellation = 9
this.form.memConstellationText = '天秤座' this.form.memConstellationText = '天秤座'
} else if ((month === 10 && day >= 23) || (month === 11 && day <= 21)) { } else if ((formattedMonth === 10 && dayInMonth >= 23) || (formattedMonth === 11 && dayInMonth <= 21)) {
this.form.memConstellation = 10 this.form.memConstellation = 10
this.form.memConstellationText = '天蝎座' this.form.memConstellationText = '天蝎座'
} else if ((month === 11 && day >= 22) || (month === 12 && day <= 21)) { } else if ((formattedMonth === 11 && dayInMonth >= 22) || (formattedMonth === 12 && dayInMonth <= 21)) {
this.form.memConstellation = 11 this.form.memConstellation = 11
this.form.memConstellationText = '射手座' this.form.memConstellationText = '射手座'
} else if ((month === 12 && day >= 22) || (month === 1 && day <= 19)) { } else if ((formattedMonth === 12 && dayInMonth >= 22) || (formattedMonth === 1 && dayInMonth <= 19)) {
this.form.memConstellation = 12 this.form.memConstellation = 12
this.form.memConstellationText = '摩羯座' this.form.memConstellationText = '摩羯座'
} }
...@@ -653,44 +711,43 @@ export default { ...@@ -653,44 +711,43 @@ export default {
// 更新城市列表为当前选中省份的城市列表 // 更新城市列表为当前选中省份的城市列表
this.$set(this.nativeActions, 1, e.value[0].children); this.$set(this.nativeActions, 1, e.value[0].children);
}, },
formatter(day) {
const d = new Date()
let month = d.getMonth() + 1
const date = d.getDate()
if (day.month == month && day.day == date + 3) {
day.bottomInfo = '有优惠'
day.dot = true
}
return day
},
codeChange(text) {
this.tips = text;
},
calendarClose() {
this.showCalendar = false
this.$refs.form1.validateField('hotel')
},
submit() { submit() {
// 如果有错误,会在catch中返回报错信息数组,校验通过则在then中返回true this.$refs.uForm.validate().then(res => {
this.$refs.form1.validate().then(res => {
uni.$u.toast('校验通过') uni.$u.toast('校验通过')
}).catch(errors => { }).catch(errors => {
uni.$u.toast('校验失败') uni.$u.toast('校验失败')
}) })
}, },
reset() { reset() {
const validateList = ['userInfo.name', 'userInfo.sex', 'radiovalue1', 'checkboxValue1', 'intro',
'hotel', 'code', 'userInfo.birthday']
this.$refs.form1.resetFields()
this.$refs.form1.clearValidate()
setTimeout(()=>{
this.$refs.form1.clearValidate(validateList)
// 或者使用 this.$refs.form1.clearValidate()
},10)
}, },
hideKeyboard() { hideKeyboard() {
uni.hideKeyboard() uni.hideKeyboard()
} },
limitHeightNum (value) {
if (value) {
const num = parseInt(value, 10)
if (num < 0) {
this.userInfo.memHeight = 0
} else if (num > 250) {
this.userInfo.memHeight = 250
} else {
this.userInfo.memHeight = num
}
}
},
limitWeightNum (value) {
if (value) {
const num = parseInt(value, 10)
if (num < 0) {
this.userInfo.memWeight = 0
} else if (num > 250) {
this.userInfo.memWeight = 250
} else {
this.userInfo.memWeight = num
}
}
},
}, },
} }
</script> </script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment