u-form.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <view class="u-form">
  3. <slot/>
  4. </view>
  5. </template>
  6. <script>
  7. /**
  8. * form 表单
  9. * @description 此组件一般用于表单场景,可以配置Input输入框,Select弹出框,进行表单验证等。
  10. * @tutorial http://uviewui.com/components/form.html
  11. * @property {Object} model 表单数据对象
  12. * @property {Boolean} border-bottom 是否显示表单域的下划线边框
  13. * @property {String} label-position 表单域提示文字的位置,left-左侧,top-上方
  14. * @property {String Number} label-width 提示文字的宽度,单位rpx(默认90)
  15. * @property {Object} label-style lable的样式,对象形式
  16. * @property {String} label-align lable的对齐方式
  17. * @property {Object} rules 通过ref设置,见官网说明
  18. * @property {Array} error-type 错误的提示方式,数组形式,见上方说明(默认['message'])
  19. * @example <u-form :model="form" ref="uForm"></u-form>
  20. */
  21. export default {
  22. name: 'u-form',
  23. props: {
  24. // 当前form的需要验证字段的集合
  25. model: {
  26. type: Object,
  27. default() {
  28. return {};
  29. }
  30. },
  31. // 验证规则
  32. // rules: {
  33. // type: [Object, Function, Array],
  34. // default() {
  35. // return {};
  36. // }
  37. // },
  38. // 有错误时的提示方式,message-提示信息,border-如果input设置了边框,变成呈红色,
  39. // border-bottom-下边框呈现红色,none-无提示
  40. errorType: {
  41. type: Array,
  42. default() {
  43. return ['message', 'toast']
  44. }
  45. },
  46. // 是否显示表单域的下划线边框
  47. borderBottom: {
  48. type: Boolean,
  49. default: true
  50. },
  51. // label的位置,left-左边,top-上边
  52. labelPosition: {
  53. type: String,
  54. default: 'left'
  55. },
  56. // label的宽度,单位rpx
  57. labelWidth: {
  58. type: [String, Number],
  59. default: 90
  60. },
  61. // lable字体的对齐方式
  62. labelAlign: {
  63. type: String,
  64. default: 'left'
  65. },
  66. // lable的样式,对象形式
  67. labelStyle: {
  68. type: Object,
  69. default() {
  70. return {}
  71. }
  72. },
  73. },
  74. provide() {
  75. return {
  76. uForm: this
  77. };
  78. },
  79. data() {
  80. return {
  81. rules: {}
  82. };
  83. },
  84. created() {
  85. // 存储当前form下的所有u-form-item的实例
  86. // 不能定义在data中,否则微信小程序会造成循环引用而报错
  87. this.fields = [];
  88. },
  89. methods: {
  90. setRules(rules) {
  91. this.rules = rules;
  92. },
  93. // 清空所有u-form-item组件的内容,本质上是调用了u-form-item组件中的resetField()方法
  94. resetFields() {
  95. this.fields.map(field => {
  96. field.resetField();
  97. });
  98. },
  99. // 校验全部数据
  100. validate(callback) {
  101. return new Promise(resolve => {
  102. // 对所有的u-form-item进行校验
  103. let valid = true; // 默认通过
  104. let count = 0; // 用于标记是否检查完毕
  105. let errorArr = []; // 存放错误信息
  106. this.fields.map(field => {
  107. // 调用每一个u-form-item实例的validation的校验方法
  108. field.validation('', error => {
  109. // 如果任意一个u-form-item校验不通过,就意味着整个表单不通过
  110. if (error) {
  111. valid = false;
  112. errorArr.push(error);
  113. }
  114. // 当历遍了所有的u-form-item时,调用promise的then方法
  115. if (++count === this.fields.length) {
  116. resolve(valid); // 进入promise的then方法
  117. // 判断是否设置了toast的提示方式,只提示最前面的表单域的第一个错误信息
  118. if (this.errorType.indexOf('none') === -1 && this.errorType.indexOf('toast') >= 0 && errorArr.length) {
  119. this.$u.toast(errorArr[0]);
  120. }
  121. // 调用回调方法
  122. if (typeof callback == 'function') callback(valid);
  123. }
  124. });
  125. });
  126. });
  127. }
  128. }
  129. };
  130. </script>
  131. <style lang="scss" scoped>
  132. @import "../../libs/css/style.components.scss";
  133. </style>