formCreate - 副本.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <template>
  2. <view class="container">
  3. <uni-section title="">
  4. <view class="example">
  5. <!-- 基础用法,不包含校验规则 -->
  6. <uni-forms ref="baseForm" :rules="rules" :model="baseFormData" labelWidth="80px" :label-position="alignment">
  7. <uni-forms-item label="姓名" required>
  8. <uni-easyinput v-model="baseFormData.name" placeholder="请输入姓名" disabled />
  9. </uni-forms-item>
  10. <uni-forms-item label="年龄" required>
  11. <uni-easyinput v-model="baseFormData.age" placeholder="请输入年龄" />
  12. </uni-forms-item>
  13. <uni-forms-item label="性别" required>
  14. <uni-data-checkbox v-model="baseFormData.sex" :localdata="sexs" />
  15. </uni-forms-item>
  16. <uni-forms-item label="兴趣爱好" required>
  17. <uni-data-checkbox v-model="baseFormData.hobby" multiple :localdata="hobbys" />
  18. </uni-forms-item>
  19. <uni-forms-item label="自我介绍">
  20. <uni-easyinput type="textarea" v-model="baseFormData.introduction" placeholder="请输入自我介绍" />
  21. </uni-forms-item>
  22. <uni-forms-item label="日期时间">
  23. <uni-datetime-picker type="datetime" return-type="timestamp"
  24. v-model="baseFormData.datetimesingle" />
  25. </uni-forms-item>
  26. <uni-forms-item label="选择城市">
  27. <uni-data-picker v-model="baseFormData.city" :localdata="cityData" popup-title="选择城市">
  28. </uni-data-picker>
  29. </uni-forms-item>
  30. <uni-forms-item label="选择技能">
  31. <uni-data-select v-model="baseFormData.skills" :localdata="skillsRange" >
  32. </uni-data-select>
  33. </uni-forms-item>
  34. </uni-forms>
  35. </view>
  36. </uni-section>
  37. <uni-section title="表单校验" type="line">
  38. <view class="example">
  39. <!-- 基础表单校验 -->
  40. <uni-forms ref="valiForm" :rules="rules" :model="valiFormData" labelWidth="80px" :label-position="alignment">
  41. <uni-forms-item label="姓名" required name="name">
  42. <uni-easyinput v-model="valiFormData.name" placeholder="请输入姓名" />
  43. </uni-forms-item>
  44. <uni-forms-item label="年龄" required name="age">
  45. <uni-easyinput v-model="valiFormData.age" placeholder="请输入年龄" />
  46. </uni-forms-item>
  47. <uni-forms-item label="自我介绍">
  48. <uni-easyinput type="textarea" v-model="valiFormData.introduction" placeholder="请输入自我介绍" />
  49. </uni-forms-item>
  50. </uni-forms>
  51. <button type="primary" @click="submit('valiForm')">提交</button>
  52. </view>
  53. </uni-section>
  54. <uni-section title="自定义校验规则" type="line">
  55. <view class="example">
  56. <!-- 自定义表单校验 -->
  57. <uni-forms ref="customForm" :rules="customRules" labelWidth="80px" :modelValue="customFormData" :label-position="alignment">
  58. <uni-forms-item label="姓名" required name="name">
  59. <uni-easyinput v-model="customFormData.name" placeholder="请输入姓名" />
  60. </uni-forms-item>
  61. <uni-forms-item label="年龄" required name="age">
  62. <uni-easyinput v-model="customFormData.age" placeholder="请输入年龄" />
  63. </uni-forms-item>
  64. <uni-forms-item label="兴趣爱好" required name="hobby">
  65. <uni-data-checkbox v-model="customFormData.hobby" multiple :localdata="hobbys" />
  66. </uni-forms-item>
  67. </uni-forms>
  68. <button type="primary" @click="submit('customForm')">提交</button>
  69. </view>
  70. </uni-section>
  71. <uni-section title="动态表单" type="line">
  72. <view class="example">
  73. <!-- 动态表单校验 -->
  74. <uni-forms ref="dynamicForm" :rules="dynamicRules" :model="dynamicFormData" labelWidth="80px" :label-position="alignment">
  75. <uni-forms-item label="邮箱" required name="email">
  76. <uni-easyinput v-model="dynamicFormData.email" placeholder="请输入姓名" />
  77. </uni-forms-item>
  78. <uni-forms-item v-for="(item,index) in dynamicFormData.domains" :key="item.id"
  79. :label="item.label+' '+index" required :rules="item.rules" :name="['domains',index,'value']">
  80. <view class="form-item">
  81. <uni-easyinput v-model="dynamicFormData.domains[index].value" placeholder="请输入域名" />
  82. <button class="button" size="mini" type="default" @click="del(item.id)">删除</button>
  83. </view>
  84. </uni-forms-item>
  85. </uni-forms>
  86. <view class="button-group">
  87. <button type="primary" size="mini" @click="add">新增域名</button>
  88. <button type="primary" size="mini" @click="submit('dynamicForm')">提交</button>
  89. </view>
  90. </view>
  91. </uni-section>
  92. </view>
  93. </template>
  94. <script>
  95. export default {
  96. data() {
  97. return {
  98. // 基础表单数据
  99. baseFormData: {
  100. name: '',
  101. age: '',
  102. introduction: '',
  103. sex: 2,
  104. hobby: [5],
  105. datetimesingle: 1627529992399,
  106. city: '',
  107. skills: 0
  108. },
  109. // 城市数据
  110. cityData: [{
  111. text: "北京",
  112. value: "10001",
  113. }, {
  114. text: "上海",
  115. value: "10002",
  116. }, {
  117. text: "深圳",
  118. value: "10004",
  119. }],
  120. skillsRange: [{
  121. value: 0,
  122. text: "编程"
  123. },
  124. {
  125. value: 1,
  126. text: "绘画"
  127. },
  128. {
  129. value: 2,
  130. text: "运动"
  131. },
  132. ],
  133. // 表单数据
  134. alignmentFormData: {
  135. name: '',
  136. age: '',
  137. },
  138. // 单选数据源
  139. sexs: [{
  140. text: '男',
  141. value: 0
  142. }, {
  143. text: '女',
  144. value: 1
  145. }, {
  146. text: '保密',
  147. value: 2
  148. }],
  149. // 多选数据源
  150. hobbys: [{
  151. text: '跑步',
  152. value: 0
  153. }, {
  154. text: '游泳',
  155. value: 1
  156. }, {
  157. text: '绘画',
  158. value: 2
  159. }, {
  160. text: '足球',
  161. value: 3
  162. }, {
  163. text: '篮球',
  164. value: 4
  165. }, {
  166. text: '其他',
  167. value: 5
  168. }],
  169. // 分段器数据
  170. current: 0,
  171. items: ['左对齐', '顶部对齐'],
  172. // 校验表单数据
  173. valiFormData: {
  174. name: '',
  175. age: '',
  176. introduction: '',
  177. },
  178. // 校验规则
  179. rules: {
  180. name: {
  181. rules: [{
  182. required: true,
  183. errorMessage: '姓名不能为空'
  184. }]
  185. },
  186. age: {
  187. rules: [{
  188. required: true,
  189. errorMessage: '年龄不能为空'
  190. }, {
  191. format: 'number',
  192. errorMessage: '年龄只能输入数字'
  193. }]
  194. }
  195. },
  196. // 自定义表单数据
  197. customFormData: {
  198. name: '',
  199. age: '',
  200. hobby: []
  201. },
  202. // 自定义表单校验规则
  203. customRules: {
  204. name: {
  205. rules: [{
  206. required: true,
  207. errorMessage: '姓名不能为空'
  208. }]
  209. },
  210. age: {
  211. rules: [{
  212. required: true,
  213. errorMessage: '年龄不能为空'
  214. }]
  215. },
  216. hobby: {
  217. rules: [{
  218. format: 'array'
  219. },
  220. {
  221. validateFunction: function(rule, value, data, callback) {
  222. if (value.length < 2) {
  223. callback('请至少勾选两个兴趣爱好')
  224. }
  225. return true
  226. }
  227. }
  228. ]
  229. }
  230. },
  231. dynamicFormData: {
  232. email: '',
  233. domains: []
  234. },
  235. dynamicLists: [],
  236. dynamicRules: {
  237. email: {
  238. rules: [{
  239. required: true,
  240. errorMessage: '域名不能为空'
  241. }, {
  242. format: 'email',
  243. errorMessage: '域名格式错误'
  244. }]
  245. }
  246. }
  247. }
  248. },
  249. computed: {
  250. // 处理表单排列切换
  251. alignment() {
  252. return 'top'
  253. }
  254. },
  255. onLoad() {},
  256. onReady() {
  257. // 设置自定义表单校验规则,必须在节点渲染完毕后执行
  258. this.$refs.customForm.setRules(this.customRules)
  259. },
  260. methods: {
  261. onClickItem(e) {
  262. console.log(e);
  263. this.current = e.currentIndex
  264. },
  265. add() {
  266. this.dynamicFormData.domains.push({
  267. label: '域名',
  268. value: '',
  269. rules: [{
  270. 'required': true,
  271. errorMessage: '域名项必填'
  272. }],
  273. id: Date.now()
  274. })
  275. },
  276. del(id) {
  277. let index = this.dynamicLists.findIndex(v => v.id === id)
  278. this.dynamicLists.splice(index, 1)
  279. },
  280. submit(ref) {
  281. console.log(this.baseFormData);
  282. this.$refs[ref].validate().then(res => {
  283. console.log('success', res);
  284. uni.showToast({
  285. title: `校验通过`
  286. })
  287. }).catch(err => {
  288. console.log('err', err);
  289. })
  290. },
  291. }
  292. }
  293. </script>
  294. <style lang="scss">
  295. .example {
  296. padding: 15px;
  297. background-color: #fff;
  298. }
  299. .segmented-control {
  300. margin-bottom: 15px;
  301. }
  302. .button-group {
  303. margin-top: 15px;
  304. display: flex;
  305. justify-content: space-around;
  306. }
  307. .form-item {
  308. display: flex;
  309. align-items: center;
  310. flex: 1;
  311. }
  312. .button {
  313. display: flex;
  314. align-items: center;
  315. height: 35px;
  316. line-height: 35px;
  317. margin-left: 10px;
  318. }
  319. </style>