quotationsForm.vue 5.0 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. {
  70. required: true,
  71. errorMessage: '手机号码不能为空'
  72. },
  73. {
  74. pattern:'^1[3456789]\\d{9}$',
  75. errorMessage: '手机号不合法'
  76. }
  77. ]
  78. },
  79. address: {
  80. rules: [{
  81. required: true,
  82. errorMessage: '地址不能为空'
  83. }]
  84. },
  85. type: {
  86. rules: [{
  87. required: true,
  88. errorMessage: '分类不能为空'
  89. }]
  90. },
  91. imgUrlList: {
  92. rules: [{
  93. required: true,
  94. errorMessage: '图片不能为空'
  95. }]
  96. },
  97. price: {
  98. rules: [{
  99. required: true,
  100. errorMessage: '年龄不能为空'
  101. }, {
  102. format: 'number',
  103. errorMessage: '年龄只能输入数字'
  104. }]
  105. }
  106. },
  107. imageList: [],
  108. customFormData:{}
  109. }
  110. },
  111. onLoad() {},
  112. onReady() {
  113. // 设置自定义表单校验规则,必须在节点渲染完毕后执行
  114. this.$refs.customForm.setRules(this.rules)
  115. },
  116. created() {
  117. this.getType();
  118. },
  119. methods: {
  120. getType(){
  121. listConfig().then(response => {
  122. this.TreeData = this.formatOptions(response.data.quotations);
  123. });
  124. },
  125. //转换为下拉选列表
  126. formatOptions(data) {
  127. return data.map(item => ({
  128. value: item.id,
  129. text: item.productName,
  130. children: item.children ? this.formatOptions(item.children) : null,
  131. }));
  132. },
  133. submit(ref) {
  134. this.$refs[ref].validate().then(res => {
  135. if (res) {
  136. this.customFormData.url = this.imageList.join(',')
  137. addQuotations(this.customFormData).then(response => {
  138. this.$modal.msgSuccess('提交成功');
  139. this.customFormData = {...this.baseFormData};
  140. this.imageList = [];
  141. uni.navigateTo({
  142. url: '../quotations/quotations'
  143. });
  144. }).catch(err => {
  145. console.log('err', err);
  146. })
  147. }
  148. })
  149. },
  150. updateImg(imgList) {
  151. this.imageList = imgList;
  152. }
  153. }
  154. }
  155. </script>
  156. <style lang="scss">
  157. .example {
  158. padding: 15px;
  159. background-color: #fff;
  160. }
  161. .uni-forms .uni-forms-item:last-child{
  162. padding-left: 4%;
  163. }
  164. .segmented-control {
  165. margin-bottom: 15px;
  166. }
  167. .button-group {
  168. margin-top: 15px;
  169. display: flex;
  170. justify-content: space-around;
  171. }
  172. .form-item {
  173. display: flex;
  174. align-items: center;
  175. }
  176. .button {
  177. display: flex;
  178. align-items: center;
  179. height: 35px;
  180. margin-left: 10px;
  181. }
  182. </style>