You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.1 KiB

2 months ago
  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. validate() {
  46. /**
  47. * 在微信小程序中通过this.$parent拿到的父组件是u--form而不是其内嵌的u-form
  48. * 导致在u-form组件中拿不到对应的children数组从而校验无效所以这里每次调用u-form组件中的
  49. * 对应方法的时候在小程序中都先将u--form的children赋值给u-form中的children
  50. */
  51. // #ifdef MP-WEIXIN
  52. this.setMpData()
  53. // #endif
  54. return this.$refs.uForm.validate()
  55. },
  56. validateField(value, callback) {
  57. // #ifdef MP-WEIXIN
  58. this.setMpData()
  59. // #endif
  60. return this.$refs.uForm.validateField(value, callback)
  61. },
  62. resetFields() {
  63. // #ifdef MP-WEIXIN
  64. this.setMpData()
  65. // #endif
  66. return this.$refs.uForm.resetFields()
  67. },
  68. clearValidate(props) {
  69. // #ifdef MP-WEIXIN
  70. this.setMpData()
  71. // #endif
  72. return this.$refs.uForm.clearValidate(props)
  73. },
  74. setMpData() {
  75. this.$refs.uForm.children = this.children
  76. }
  77. },
  78. }
  79. </script>