quotationsForm.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <uni-section title="自定义校验规则" type="line">
  3. <view class="example">
  4. <!-- 自定义表单校验 -->
  5. <uni-forms ref="customForm" :rules="rules" :modelValue="customFormData" label-align="center">
  6. <uni-forms-item label="产品名称" required name="productName" label-width="85" label-align="center">
  7. <uni-easyinput v-model="customFormData.productName" placeholder="请输入产品名称" />
  8. </uni-forms-item>
  9. <uni-forms-item label="地区" required name="area" >
  10. <uni-easyinput v-model="customFormData.area" placeholder="请输入地区" />
  11. </uni-forms-item>
  12. <uni-forms-item label="价格" required name="price" >
  13. <uni-easyinput v-model="customFormData.price" placeholder="请输入价格" />
  14. </uni-forms-item>
  15. <uni-forms-item label="手机号" required name="phone" >
  16. <uni-easyinput v-model="customFormData.phone" placeholder="请输入手机号" />
  17. </uni-forms-item>
  18. <uni-forms-item label="地址" required name="address">
  19. <uni-easyinput v-model="customFormData.address" placeholder="请输入地址" />
  20. </uni-forms-item>
  21. <uni-forms-item label="类型" required name="type" >
  22. <uni-data-picker placeholder="请选择分类" popup-title="请选择所属分类" :localdata="TreeData" v-model="customFormData.type"/>
  23. </uni-forms-item>
  24. <uni-forms-item label="图片" required name="url" label-width="85">
  25. <upload :imgArr="imageList" :fileSize="1" :limit="3" @updateImg="updateImg"/>
  26. </uni-forms-item>
  27. </uni-forms>
  28. <button type="primary" @click="submit('customForm')">提交</button>
  29. </view>
  30. </uni-section>
  31. </template>
  32. <script>
  33. import {listConfig,addQuotations} from "@/api/quotations/quotations";
  34. import upload from '../../components/upload/index.vue'
  35. export default {
  36. components: {
  37. upload
  38. },
  39. data() {
  40. return {
  41. // 基础表单数据
  42. baseFormData: {
  43. productName: '',
  44. area:'',
  45. price:'',
  46. phone:'',
  47. address:'',
  48. type:'',
  49. url:[]
  50. },
  51. //树数据
  52. TreeData:[],
  53. // 校验规则
  54. rules: {
  55. productName: {
  56. rules: [{
  57. required: true,
  58. errorMessage: '产品名称不能为空'
  59. }]
  60. },
  61. area: {
  62. rules: [{
  63. required: true,
  64. errorMessage: '地区不能为空'
  65. }]
  66. },
  67. phone: {
  68. rules: [{
  69. required: true,
  70. errorMessage: '年龄不能为空'
  71. }, {
  72. validateFunction: function(rule, value, data, callback) {
  73. const reg = /^1[3-9]\.\d{9}$/;
  74. if (!reg.test(value)) {
  75. callback('请输入正确的手机号码');
  76. }
  77. return true;
  78. }
  79. }]
  80. },
  81. address: {
  82. rules: [{
  83. required: true,
  84. errorMessage: '地址不能为空'
  85. }]
  86. },
  87. type: {
  88. rules: [{
  89. required: true,
  90. errorMessage: '分类不能为空'
  91. }]
  92. },
  93. imgUrlList: {
  94. rules: [{
  95. required: true,
  96. errorMessage: '图片不能为空'
  97. }]
  98. },
  99. price: {
  100. rules: [{
  101. required: true,
  102. errorMessage: '年龄不能为空'
  103. }, {
  104. format: 'number',
  105. errorMessage: '年龄只能输入数字'
  106. }]
  107. }
  108. },
  109. imageList: [],
  110. customFormData:{}
  111. }
  112. },
  113. onLoad() {},
  114. onReady() {
  115. // 设置自定义表单校验规则,必须在节点渲染完毕后执行
  116. this.$refs.customForm.setRules(this.rules)
  117. },
  118. created() {
  119. this.getType();
  120. },
  121. methods: {
  122. getType(){
  123. listConfig().then(response => {
  124. this.TreeData = this.formatOptions(response.data.quotations);
  125. });
  126. },
  127. //转换为下拉选列表
  128. formatOptions(data) {
  129. return data.map(item => ({
  130. value: item.id,
  131. text: item.productName,
  132. children: item.children ? this.formatOptions(item.children) : null,
  133. }));
  134. },
  135. submit(ref) {
  136. this.$refs[ref].validate().then(res => {
  137. if (res) {
  138. this.customFormData.url = this.imageList.join(',')
  139. addQuotations(this.customFormData).then(response => {
  140. this.$modal.msgSuccess('提交成功');
  141. this.customFormData = {...this.baseFormData};
  142. this.imageList = [];
  143. uni.navigateTo({
  144. url: '../quotations/quotations'
  145. });
  146. }).catch(err => {
  147. console.log('err', err);
  148. })
  149. }
  150. })
  151. },
  152. updateImg(imgList) {
  153. this.imageList = imgList;
  154. }
  155. }
  156. }
  157. </script>
  158. <style lang="scss">
  159. .example {
  160. padding: 15px;
  161. background-color: #fff;
  162. }
  163. .segmented-control {
  164. margin-bottom: 15px;
  165. }
  166. .button-group {
  167. margin-top: 15px;
  168. display: flex;
  169. justify-content: space-around;
  170. }
  171. .form-item {
  172. display: flex;
  173. align-items: center;
  174. }
  175. .button {
  176. display: flex;
  177. align-items: center;
  178. height: 35px;
  179. margin-left: 10px;
  180. }
  181. </style>