u--form.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <uvForm
  3. ref="uForm"
  4. :model="model"
  5. :rules="rules"
  6. :errorType="errorType"
  7. :borderBottom="borderBottom"
  8. :labelPosition="labelPosition"
  9. :labelWidth="labelWidth"
  10. :labelAlign="labelAlign"
  11. :labelStyle="labelStyle"
  12. :customStyle="customStyle"
  13. >
  14. <slot />
  15. </uvForm>
  16. </template>
  17. <script>
  18. /**
  19. * 此组件存在的理由是,在nvue下,u-form被uni-app官方占用了,u-form在nvue中相当于form组件
  20. * 所以在nvue下,取名为u--form,内部其实还是u-form.vue,只不过做一层中转
  21. */
  22. import uvForm from '../u-form/u-form.vue';
  23. import { props } from '../u-form/props.js';
  24. import { mpMixin } from '../../libs/mixin/mpMixin';
  25. import { mixin } from '../../libs/mixin/mixin';
  26. export default {
  27. // #ifdef MP-WEIXIN
  28. name: 'u-form',
  29. // #endif
  30. // #ifndef MP-WEIXIN
  31. name: 'u--form',
  32. // #endif
  33. mixins: [mpMixin, props, mixin],
  34. components: {
  35. uvForm
  36. },
  37. created() {
  38. this.children = []
  39. },
  40. methods: {
  41. // 手动设置校验的规则,如果规则中有函数的话,微信小程序中会过滤掉,所以只能手动调用设置规则
  42. setRules(rules) {
  43. this.$refs.uForm.setRules(rules)
  44. },
  45. /**
  46. * 校验全部数据
  47. * @param {Object} options
  48. * @param {Boolean} options.showErrorMsg -是否显示校验信息,
  49. */
  50. validate(options) {
  51. /**
  52. * 在微信小程序中,通过this.$parent拿到的父组件是u--form,而不是其内嵌的u-form
  53. * 导致在u-form组件中,拿不到对应的children数组,从而校验无效,所以这里每次调用u-form组件中的
  54. * 对应方法的时候,在小程序中都先将u--form的children赋值给u-form中的children
  55. */
  56. // #ifdef MP-WEIXIN
  57. this.setMpData()
  58. // #endif
  59. return this.$refs.uForm.validate(options)
  60. },
  61. validateField(value, callback) {
  62. // #ifdef MP-WEIXIN
  63. this.setMpData()
  64. // #endif
  65. return this.$refs.uForm.validateField(value, callback)
  66. },
  67. resetFields() {
  68. // #ifdef MP-WEIXIN
  69. this.setMpData()
  70. // #endif
  71. return this.$refs.uForm.resetFields()
  72. },
  73. clearValidate(props) {
  74. // #ifdef MP-WEIXIN
  75. this.setMpData()
  76. // #endif
  77. return this.$refs.uForm.clearValidate(props)
  78. },
  79. setMpData() {
  80. this.$refs.uForm.children = this.children
  81. }
  82. },
  83. }
  84. </script>