form.vue 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <view>
  3. <page-head title="form"></page-head>
  4. <view class="uni-padding-wrap uni-common-mt">
  5. <form @submit="formSubmit" @reset="formReset">
  6. <view class="uni-form-item uni-column">
  7. <view class="title">姓名</view>
  8. <input class="uni-input" name="nickname" placeholder="请输入姓名" />
  9. </view>
  10. <view class="uni-form-item uni-column">
  11. <view class="title">性别</view>
  12. <radio-group name="gender">
  13. <label>
  14. <radio value="男" /><text>男</text>
  15. </label>
  16. <label>
  17. <radio value="女" /><text>女</text>
  18. </label>
  19. </radio-group>
  20. </view>
  21. <view class="uni-form-item uni-column">
  22. <view class="title">爱好</view>
  23. <checkbox-group name="loves">
  24. <label>
  25. <checkbox value="读书" /><text>读书</text>
  26. </label>
  27. <label>
  28. <checkbox value="写字" /><text>写字</text>
  29. </label>
  30. </checkbox-group>
  31. </view>
  32. <view class="uni-form-item uni-column">
  33. <view class="title">年龄</view>
  34. <slider value="20" name="age" show-value></slider>
  35. </view>
  36. <view class="uni-form-item uni-column">
  37. <view class="title">保留选项</view>
  38. <view>
  39. <switch name="switch" />
  40. </view>
  41. </view>
  42. <view class="uni-btn-v">
  43. <button form-type="submit">Submit</button>
  44. <button type="default" form-type="reset">Reset</button>
  45. </view>
  46. </form>
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. var graceChecker = require("../../../common/graceChecker.js");
  52. export default {
  53. data() {
  54. return {
  55. }
  56. },
  57. methods: {
  58. formSubmit: function(e) {
  59. console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e.detail.value))
  60. //定义表单规则
  61. var rule = [
  62. {name:"nickname", checkType : "string", checkRule:"1,3", errorMsg:"姓名应为1-3个字符"},
  63. {name:"gender", checkType : "in", checkRule:"男,女", errorMsg:"请选择性别"},
  64. {name:"loves", checkType : "notnull", checkRule:"", errorMsg:"请选择爱好"}
  65. ];
  66. //进行表单检查
  67. var formData = e.detail.value;
  68. var checkRes = graceChecker.check(formData, rule);
  69. if(checkRes){
  70. uni.showToast({title:"验证通过!", icon:"none"});
  71. }else{
  72. uni.showToast({ title: graceChecker.error, icon: "none" });
  73. }
  74. },
  75. formReset: function(e) {
  76. console.log('清空数据')
  77. }
  78. }
  79. }
  80. </script>
  81. <style>
  82. .uni-form-item .title {
  83. padding: 20rpx 0;
  84. }
  85. </style>