默认配置使用

    一个完整的包含所有的内置表单相关组件。

    1. export default {
    2. data() {
    3. return {
    4. validity: {},
    5. valid: undefined,
    6. model: {
    7. checkboxValue: false,
    8. checkboxGroupValue: [],
    9. inputValue: '',
    10. radioValue: '',
    11. rateValue: 0,
    12. selectValue: 2018,
    13. switchValue: true,
    14. textareaValue: '',
    15. uploadValue: []
    16. },
    17. schema: {
    18. groups: [
    19. {
    20. legend: '基础',
    21. fields: [
    22. {
    23. type: 'checkbox',
    24. modelKey: 'checkboxValue',
    25. props: {
    26. option: {
    27. label: 'Checkbox',
    28. value: true
    29. }
    30. },
    31. rules: {
    32. required: true
    33. },
    34. messages: {
    35. required: 'Please check this field'
    36. }
    37. },
    38. {
    39. type: 'checkbox-group',
    40. modelKey: 'checkboxGroupValue',
    41. label: 'CheckboxGroup',
    42. props: {
    43. options: ['1', '2', '3']
    44. },
    45. rules: {
    46. required: true
    47. }
    48. },
    49. {
    50. type: 'input',
    51. modelKey: 'inputValue',
    52. label: 'Input',
    53. props: {
    54. placeholder: '请输入'
    55. },
    56. rules: {
    57. required: true
    58. },
    59. // validating when blur
    60. trigger: 'blur'
    61. },
    62. {
    63. type: 'radio-group',
    64. modelKey: 'radioValue',
    65. label: 'Radio',
    66. props: {
    67. options: ['1', '2', '3']
    68. },
    69. rules: {
    70. required: true
    71. }
    72. },
    73. {
    74. type: 'select',
    75. modelKey: 'selectValue',
    76. label: 'Select',
    77. props: {
    78. options: [2015, 2016, 2017, 2018, 2019, 2020]
    79. },
    80. rules: {
    81. required: true
    82. }
    83. },
    84. {
    85. type: 'switch',
    86. modelKey: 'switchValue',
    87. label: 'Switch',
    88. rules: {
    89. required: true
    90. }
    91. },
    92. {
    93. type: 'textarea',
    94. modelKey: 'textareaValue',
    95. label: 'Textarea',
    96. rules: {
    97. },
    98. // debounce validate
    99. // if set to true, the default debounce time will be 200(ms)
    100. debounce: 100
    101. }
    102. ]
    103. },
    104. {
    105. legend: '高级',
    106. fields: [
    107. {
    108. type: 'rate',
    109. modelKey: 'rateValue',
    110. label: 'Rate',
    111. required: true
    112. }
    113. },
    114. {
    115. type: 'upload',
    116. modelKey: 'uploadValue',
    117. label: 'Upload',
    118. events: {
    119. 'file-removed': (...args) => {
    120. console.log('file removed', args)
    121. }
    122. },
    123. rules: {
    124. required: true,
    125. uploaded: (val, config) => {
    126. return Promise.all(val.map((file, i) => {
    127. return new Promise((resolve, reject) => {
    128. if (file.uploadedUrl) {
    129. return resolve()
    130. }
    131. // fake request
    132. setTimeout(() => {
    133. if (i % 2) {
    134. reject(new Error())
    135. } else {
    136. file.uploadedUrl = 'uploaded/url'
    137. resolve()
    138. }
    139. }, 1000)
    140. })
    141. })).then(() => {
    142. return true
    143. })
    144. }
    145. },
    146. messages: {
    147. uploaded: '上传失败'
    148. }
    149. }
    150. ]
    151. },
    152. {
    153. fields: [
    154. {
    155. type: 'submit',
    156. label: 'Submit'
    157. },
    158. {
    159. type: 'reset',
    160. label: 'Reset'
    161. }
    162. ]
    163. }
    164. ]
    165. },
    166. options: {
    167. scrollToInvalidField: true,
    168. layout: 'standard' // classic fresh
    169. }
    170. }
    171. },
    172. methods: {
    173. submitHandler(e) {
    174. e.preventDefault()
    175. console.log('submit', e)
    176. },
    177. validateHandler(result) {
    178. this.validity = result.validity
    179. this.valid = result.valid
    180. console.log('validity', result.validity, result.valid, result.dirty, result.firstInvalidFieldIndex)
    181. },
    182. resetHandler(e) {
    183. console.log('reset', e)
    184. }
    185. }
    186. }

    model 就是整个表单需要的数据源,schema 就是生成表单所定义的模式,immediate-validate 如果为 true 则初始时立即做校验,options 则是配置选项。

    submit 校验成功后提交事件,validate 每次有数据校验更新的事件,reset 则是重置事件。

    自定义使用

    1. // province, city, area
    2. // select component
    3. const PCA = {
    4. props: {
    5. value: {
    6. type: Array,
    7. default() {
    8. return []
    9. }
    10. }
    11. },
    12. data() {
    13. return {
    14. selected: []
    15. }
    16. },
    17. render(createElement) {
    18. return createElement('cube-button', {
    19. on: {
    20. }
    21. }, this.selected.length ? this.selected.join(' ') : 'placeholder')
    22. },
    23. mounted() {
    24. this.picker = this.- createCascadePicker({
    25. title: 'PCA Select',
    26. data: cityData,
    27. selectedIndex: this.value,
    28. onSelect: this.selectHandler
    29. })
    30. },
    31. methods: {
    32. showPicker() {
    33. this.picker.show()
    34. },
    35. this.selected = selectedTxt
    36. this.- emit('input', selectedVal)
    37. }
    38. }
    39. }
    40. export default {
    41. data() {
    42. return {
    43. validity: {},
    44. valid: undefined,
    45. model: {
    46. inputValue: '',
    47. pcaValue: [],
    48. dateValue: ''
    49. },
    50. fields: [
    51. {
    52. type: 'input',
    53. modelKey: 'inputValue',
    54. label: 'Input',
    55. props: {
    56. placeholder: '请输入'
    57. },
    58. rules: {
    59. required: true
    60. }
    61. },
    62. {
    63. component: PCA,
    64. modelKey: 'pcaValue',
    65. label: 'PCASelect',
    66. rules: {
    67. required: true
    68. },
    69. messages: {
    70. required: '请选择'
    71. }
    72. },
    73. {
    74. modelKey: 'dateValue',
    75. label: 'Date',
    76. rules: {
    77. required: true
    78. }
    79. }
    80. ]
    81. }
    82. },
    83. methods: {
    84. submitHandler(e) {
    85. console.log('submit')
    86. },
    87. validateHandler(result) {
    88. this.validity = result.validity
    89. this.valid = result.valid
    90. console.log('validity', result.validity, result.valid, result.dirty, result.firstInvalidFieldIndex)
    91. },
    92. showDatePicker() {
    93. this.- refs.datePicker.show()
    94. },
    95. dateSelectHandler(selectedVal) {
    96. this.model.dateValue = new Date(selectedVal[0], selectedVal[1] - 1, selectedVal[2]).toDateString()
    97. }
    98. },
    99. components: {
    100. DatePicker
    101. }
    102. }

    可以通过 component 指定实现了 v-model 的自定义组件,例如示例中的 PCA 组件;也可以通过使用插槽自定义结构行为,比如示例中的日期选择。

    CubeForm

    schema 子配置项

    模式用于定义表单中的各个字段,可以选择是否分组。

    无分组

    直接包含 fields 即可:

    有分组

    1. {
    2. groups: [
    3. {
    4. legend: 'Group 1'
    5. fields: [
    6. {
    7. type: 'input',
    8. modelKey: 'inputValue',
    9. label: 'Input'
    10. },
    11. // ...
    12. ]
    13. },
    14. {
    15. legend: 'Group 2'
    16. fields: [
    17. {
    18. type: 'input',
    19. modelKey: 'inputValue',
    20. label: 'Input'
    21. },
    22. // ...
    23. ]
    24. }
    25. ]
    26. }

    不管有没有分组,我们都需要使用 fields 定义表单字段,其中每一项可以有如下属性:

    参数 说明 类型 可选值 默认值
    type 字段类型 String 默认内置的可选类型组件有:button, checkbox, checkbox-group, input, radio, radio-group, rate, select, switch, textarea, upload;以及特殊的 submitreset,它们两个会被转换为对应类型的 button -
    component 字段使用的自定义组件,替换 type,该组件组件实现 v-model Object/String - -
    modelKey 在表单的 model 数据源对象中所对应的 key 名字 String - -
    label 字段的标签值 String - -
    props type 对应的组件或者自定义组件 component 所需要的 props Object - -
    events1.8.0+ type 对应的组件或者自定义组件 component 的事件回调 Object - -
    rules 字段的校验规则,参见 Object - -
    trigger1.8.0+ 如果设置为 ‘blur’ 那么则会在离焦后校验 String blur/change -
    debounce1.8.0+ 控制校验节奏,值为时间,单位 ms。如果 trigger 设置为 blur 则此项配置不生效 Number/Boolean >= 0,如果设置为 true,那么时间就是 200(ms) -
    messages 字段的校验消息,参见 Validator String - -

    CubeFormGroup

    CubeFormItem

    参数 说明 类型 可选值 默认值
    field 字段数据 Object - -

    validate 事件的参数

    参数 说明 类型
    validity 校验结果 Object
    valid 校验合法,如果还没校验则为 undefined,一旦校验则为 true 或 false Boolean/Undefined
    invalid 校验不合法,如果还没校验则为 undefined,一旦校验则为 true 或 false Boolean
    dirty 表单处于 dirty 状态,也就意味着数据源发生了变化 Boolean
    firstInvalidFieldIndex 第一个校验不合法的字段索引值 Number

    校验结果 validity 对象

    方法名 说明 参数 返回值
    submit 提交表单 - -
    reset 重置表单 - -
    validate(cb) 校验表单 cb: 校验完成后回调函数,主要用于异步校验场景,调用参数为 valid 的值 如果支持 Promise 的话返回值是 Promise 对象(只有 resolved 状态,值为 valid),否则 undefined

    原文: