Form 表单

使用指南

在 page.json 中引入组件

  1. "usingComponents": {
  2. "w-cell": "/dist/w-cell/index",
  3. "w-cell-group": "/dist/w-cell-group/index",
  4. "w-input": "/dist/w-input/index",
  5. "w-checkbox": "/dist/w-checkbox/index",
  6. "w-button": "/dist/w-button/index",
  7. "w-form": "/dist/w-form/index",
  8. "w-switch": "/dist/w-switch/index",
  9. "w-radio": "/dist/w-radio/index",
  10. "w-date-picker": "/dist/w-date-picker/index",
  11. "w-address-picker": "/dist/w-address-picker/index",
  12. "w-cell-picker": "/dist/w-cell-picker/index",
  13. "w-number": "/dist/w-number/index",
  14. "w-vcode": "/dist/w-vcode/index",
  15. "w-validate": "/dist/w-validate/index",
  16. "w-pane": "/dist/w-pane/index"
  17. }

视频演示

表单域

只要表单组件包裹在 w-form 表单里,表单域可以是输入控件,标准表单域,标签,下拉菜单,文本域等。

  1. <w-form
  2. bind:onSubmit="通过onSubmit事件采集form表单域中的表单数据"
  3. bind:onReset="重置表单域中的所有数据"
  4. >
  5. 里面可以放各种表单组件
  6. 需要收集的表单组件一定要加name属性,不然form不会收集没有name字段的数据
  7. <w-input name="input" />
  8. <w-checkbox name="input"/>
  9. 表单按钮可以指定属性[form-type]来触发w-form表单的submit事件进行采集数据
  10. <w-button formType="submit"/>
  11. <w-button formType="reset"/>
  12. w-validate是表单的校验组件,在需要校验组件的外边加上w-validate组件可以对该组件进行校验,
  13. w-validate内置了常用的校验规则,用户也可自定义规则
  14. <w-validate rules="{{{ required: { value: true, message: '必填'}}}">
  15. <w-input name="validateInput" />
  16. </w-validate>
  17. <w-switch />
  18. <w-radio />
  19. </w-form>

代码演示

  1. <w-form
  2. bind:onSubmit="wussFormSubmit"
  3. bind:onReset="wussFormReset"
  4. isValidateBtn
  5. >
  6. <w-cell-group>
  7. <w-validate rules="{{ userNameRules }}">
  8. <w-input
  9. name="userName"
  10. placeholder="请输入你的姓名"
  11. clear
  12. label="姓名"
  13. />
  14. </w-validate>
  15. <w-validate rules="{{ { mode:['mobile'] } }}">
  16. <w-input
  17. type="mobile"
  18. name="mobile"
  19. clear
  20. placeholder="请输入你的手机号"
  21. label="手机号"
  22. />
  23. </w-validate>
  24. <w-validate firstValidate rules="{{ isRequired }}">
  25. <w-cell-picker
  26. label="性别"
  27. title="请选择性别"
  28. placeholder="请选择性别"
  29. options="{{ [['男','女']] }}"
  30. name="sex"
  31. bind:onOpen="handlePickerOpen"
  32. bind:onCancel="handlePickerCancel"
  33. />
  34. </w-validate>
  35. <w-validate firstValidate rules="{{ isRequired }}">
  36. <w-address-picker
  37. label="选择地区"
  38. name="region"
  39. />
  40. </w-validate>
  41. <view
  42. hidden="{{ textarea_visible }}"
  43. class=""
  44. hover-class="none"
  45. hover-stop-propagation="false"
  46. >
  47. <w-validate rules="{{ isRequired }}">
  48. <w-input
  49. clear
  50. type="textarea"
  51. label="地址"
  52. name="address"
  53. placeholder="请输入详细地址..."
  54. />
  55. </w-validate>
  56. </view>
  57. <w-cell label="同意用户协议信息">
  58. <w-switch
  59. name="isVip"
  60. slot="content"
  61. />
  62. </w-cell>
  63. <w-validate firstValidate rules="{{ isRequired }}">
  64. <w-date-picker
  65. name="createTime"
  66. mode="dateTime"
  67. label="出生日期"
  68. title="请选择日期"
  69. />
  70. </w-validate>
  71. <w-radio
  72. label="请选择手机"
  73. name="radio"
  74. defaultValue="huawei"
  75. options="{{ options1 }}"
  76. />
  77. <w-checkbox
  78. name="checkbox"
  79. options="{{ items1 }}"
  80. />
  81. <w-cell>
  82. <w-button
  83. inline
  84. type="info"
  85. formType="submit"
  86. >
  87. 提交
  88. </w-button>
  89. <w-button
  90. inline
  91. type="warn"
  92. formType="reset"
  93. >
  94. 重置
  95. </w-button>
  96. </w-cell>
  97. </w-cell-group>
  98. </w-form>
  1. data: {
  2. userNameRules: {
  3. maxLength: {
  4. value: 6,
  5. message: '姓名最多6个字',
  6. },
  7. minLength: {
  8. value: 3,
  9. message: '姓名最少三个字',
  10. },
  11. },
  12. isRequired: {
  13. required: {
  14. value: true,
  15. message: '必填',
  16. },
  17. },
  18. options1: [
  19. {
  20. key: '苹果',
  21. value: 'iphone',
  22. },
  23. {
  24. key: '华为',
  25. value: 'huawei',
  26. },
  27. {
  28. key: 'oppo音乐手机',
  29. value: 'oppo',
  30. },
  31. ],
  32. items1: [
  33. {
  34. text: '复选框1',
  35. ...MOCK_DATA,
  36. checked: true,
  37. },
  38. {
  39. text: '复选框2',
  40. ...MOCK_DATA,
  41. checked: true,
  42. disabled: true,
  43. },
  44. {
  45. text: '复选框3',
  46. ...MOCK_DATA,
  47. },
  48. ],
  49. textarea_visible: true,
  50. },
  51. handlePickerOpen() {
  52. this.setData({ textarea_visible: true })
  53. },
  54. handlePickerCancel() {
  55. this.setData({ textarea_visible: false })
  56. },
  57. wussFormSubmit(e) {
  58. console.log('提交了:', e.detail);
  59. },
  60. wussFormReset(e) {
  61. console.log('重置了:', e.detail);
  62. },

API Form

Attribute 属性

属性说明类型默认值
isValidateBtn设置提交按钮跟随验证 disabledbooleanfalse

Event 事件

事件名说明参数
submit表单提交事件e.detail
reset表单重置事件e.detail

Slot 插槽

名称说明
slot表单组件

Class 自定义类名

类名说明
wuss-class根节点样式类

w-validate 表单域校验组件

用于表单组件的数据校验,内置了常用规则。 用户也可自定义规则进行校验。

Attribute 属性
属性说明类型示例
rules校验规则对象object{ required: { value: true, message: '必填' } }
rules.required.value校验规则的值any-
rules.required.message当前校验不通过时的错误提示String-
firstValidate是否需要初始化校验booleanfalse
rules 常用校验规则
参数说明类型默认值
mode常用校验模式有 [mobile / email / url / idcard]Array-
required是否必填Boolean-
equalLength等于字符长度Number-
maxLength最大字符长度Number-
minLength最小字符长度Number-
rangelength限制字符长度的范围Array-
equal等于某值Number-
max最大值Number-
min最小值Number-
range值的范围Number-
regexp自定义校验规则正则Regexp-

使用方法

  1. <w-form>
  2. 使用mode 为mobile时,为校验手机号码
  3. <w-validate rules="{{{mode: ['mobile']}}}"> ... </w-validate>
  4. required 必填
  5. <w-validate rules="{{{required: true}}}"> ... </w-validate>
  6. regexp 自定义校验规则
  7. <w-validate rules="{{{regexp:['1','g']}}}"> ... </w-validate>
  8. </w-form>