当前位置:   article > 正文

小程序vant--form表单与验证_vant form

vant form

html

  1. <view class="id_infor_enter">
  2. <!-- 输入框列表--开始 -->
  3. <van-cell-group class="id_infor_enter_list">
  4. <van-field value="{{name}}" placeholder="请输入姓名" border="{{ true }}" bind:change="onChange" data-input="name" label="姓名" input-align="right" required="{{ true }}" size="large" is-link="{{ true }}" error-message="{{nameErr}}" error-message-align="right" :rules="userFrom.tel"/>
  5. <van-field value="{{ form.sex }}" placeholder="请选择" border="{{ true }}" label="性别" input-align="right"
  6. required="{{ true }}" size="large" is-link="{{ true }}" bind:tap="showPopup" readonly error-message="{{sexErr}}" error-message-align="right" data-picker="sex"/>
  7. <van-field value="{{ form.birthday }}" placeholder="请选择" border="{{ true }}" label="出生日期" input-align="right" required="{{ true }}" size="large" is-link="{{ true }}" bind:tap="showdata" readonly error-message="{{birthdayErr}}" error-message-align="right"/>
  8. <van-field value="{{ tel }}" placeholder="请输入手机号" border="{{ true }}" bind:change="onChange" data-input="tel" label="手机号" input-align="right" required="{{ true }}" size="large" is-link="{{ true }}" error-message="{{telErr}}" error-message-align="right"/>
  9. </van-cell-group>
  10. <!-- 输入框列表--结束 -->
  11. <!-- 提交按钮--开始 -->
  12. <van-button round type="info" class="submit_btn" bind:tap="submit_btn" block>确认</van-button>
  13. <!-- 提交按钮--结束 -->
  14. <!-- 正常选择器--开始 -->
  15. <!-- 传值为showPopup,pickertype -->
  16. <picker showPopup="{{showPopup}}" pickertype="{{pickertype}}" columns="{{columns}}" bindpicker="picker"></picker>
  17. <!-- 正常选择器--结束 -->
  18. <!-- 日期选择器--开始 -->
  19. <data_picker showdata="{{showdata}}" bindpickerdata="pickerdata"></data_picker>
  20. <!-- 日期选择器--结束 -->
  21. <van-toast id="van-toast" />
  22. </view>
  1. import Toast from '@vant/weapp/toast/toast';
  2. Page({
  3. data: {
  4. // 表单信息
  5. form:{
  6. name:'',
  7. sex:'',
  8. birthday:'',
  9. tel:'',
  10. },
  11. // 表单验证信息
  12. formrules:{
  13. nameErr:'',
  14. sexErr:'',
  15. birthdayErr:'',
  16. telErr:'',
  17. },
  18. // 控制单选picker弹出
  19. showPopup:false,
  20. // 控制日期picker弹出
  21. showdata:false,
  22. //控制日期picker父传子参数
  23. pickertype:'',
  24. // 单一picker假数据
  25. columns:[],
  26. columns1:["性别"],
  27. checked: true,
  28. },
  29. // 输入框方法
  30. onChange(event) {
  31. // 用switch控制信息变化,方法名为data-input,绑定于id_infor_list.wxml
  32. switch(event.currentTarget.dataset['input']) {
  33. case 'name':
  34. let name = event.detail
  35. this.setData({
  36. "form.name":name,
  37. })
  38. break;
  39. case 'tel':
  40. let tel = event.detail
  41. this.setData({
  42. "form.tel":tel,
  43. })
  44. break;
  45. }
  46. },
  47. // 控制向picker组件传递的值,方法名为data-picker,绑定于id_infor_list.wxml
  48. showPopup(event){
  49. // 获取van-field内参数
  50. let pickertype = event.currentTarget.dataset['picker']
  51. this.setData({
  52. showPopup:true,
  53. pickertype:pickertype,
  54. columns:this.data.columns1
  55. })
  56. // 控制数组假数据
  57. switch(pickertype) {
  58. case 'sex':
  59. this.setData({
  60. columns:this.data.columns1
  61. })
  62. break;
  63. }
  64. },
  65. // 接受子组件传递的值用pickertype判断信息的改变
  66. picker(event) {
  67. switch(event.detail.pickertype) {
  68. case 'sex':
  69. let sex = event.detail.pickerdetail
  70. this.setData({ "form.sex":sex,})
  71. break;
  72. }
  73. },
  74. // 获取子组件传值
  75. pickerdata(event){
  76. let pickerbirthday = event.detail.birthday
  77. // const { birthday } =this.data.form.birthday
  78. this.setData({
  79. "form.birthday":pickerbirthday
  80. })
  81. },
  82. // 用于展示日期选择器
  83. showdata(){
  84. this.setData({ showdata: true });
  85. },
  86. // 提交按钮--引入checkFn方法做表单验证
  87. submit_btn(){
  88. this.checkFn('name','nameErr','昵称不能为空')
  89. this.checkFn('sex','sexErr','性别不能为空')
  90. this.checkFn('birthday','birthdayErr','出生日期不能为空')
  91. this.checkFn('tel','telErr','手机号不能为空')
  92. if(this.data.form.name.trim()&&this.data.form.sex.trim()&&this.data.form.birthday.trim()&&this.data.form.tel.trim()&&this.data.checked){
  93. Toast('跳转');
  94. console.log("1111111111111")
  95. }else{
  96. Toast('请输入完整信息');
  97. console.log("2222222222")
  98. }
  99. },
  100. // checkFn方法表单验证
  101. checkFn(err,errKey,errStr){
  102. if(!this.data.form[err].trim()){
  103. this.setData({[errKey]:errStr})
  104. return false
  105. }else{
  106. this.setData({ [errKey]:''})
  107. return true
  108. }
  109. },
  110. });

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/834707
推荐阅读
相关标签
  

闽ICP备14008679号