form.nvue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <template>
  2. <view class="u-page">
  3. <up-navbar
  4. title="表单"
  5. @leftClick="navigateBack"
  6. safeAreaInsetTop
  7. fixed
  8. placeholder
  9. ></up-navbar>
  10. <view class="u-demo-block">
  11. <text class="u-demo-block__title">基础使用</text>
  12. <view class="u-demo-block__content">
  13. <!-- 注意,如果需要兼容微信小程序,最好通过setRules方法设置rules规则 -->
  14. <up-form
  15. labelPosition="left"
  16. :model="model1"
  17. ref="form1"
  18. >
  19. <up-form-item
  20. label="姓名"
  21. prop="userInfo.name"
  22. borderBottom
  23. ref="item1"
  24. >
  25. <up-input
  26. v-model="model1.userInfo.name"
  27. border="none"
  28. placeholder="姓名,只能为中文"
  29. ></up-input>
  30. </up-form-item>
  31. <up-form-item
  32. label="性别"
  33. prop="userInfo.sex"
  34. borderBottom
  35. @click="showSex = true; hideKeyboard()"
  36. ref="item1"
  37. >
  38. <view @click.self="showSex = true; hideKeyboard()">
  39. <up-input
  40. v-model="model1.userInfo.sex"
  41. disabled
  42. disabledColor="#ffffff"
  43. placeholder="请选择性别"
  44. border="none"
  45. ></up-input>
  46. </view>
  47. <template #right>
  48. <up-icon
  49. name="arrow-right"
  50. ></up-icon>
  51. </template>
  52. </up-form-item>
  53. <up-form-item
  54. label="年龄"
  55. prop="userInfo.age"
  56. borderBottom
  57. ref="item1"
  58. :rules="[{
  59. type: 'string',
  60. required: true,
  61. message: '请填写年龄',
  62. trigger: ['blur', 'change']
  63. }]"
  64. >
  65. <up-input
  66. placeholder="请输入内容"
  67. border="surround"
  68. type="number"
  69. v-model="model1.userInfo.age"
  70. clearable
  71. ></up-input>
  72. </up-form-item>
  73. <up-form-item
  74. label="水果"
  75. prop="radiovalue1"
  76. borderBottom
  77. ref="item2"
  78. >
  79. <up-radio-group v-model="model1.radiovalue1">
  80. <up-radio
  81. :customStyle="{marginRight: '16px'}"
  82. v-for="(item, index) in radiolist1"
  83. :key="index"
  84. :label="item.name"
  85. :name="item.name"
  86. >
  87. </up-radio>
  88. </up-radio-group>
  89. </up-form-item>
  90. <up-form-item
  91. label="兴趣爱好"
  92. prop="checkboxValue1"
  93. borderBottom
  94. labelWidth="80"
  95. ref="item3"
  96. >
  97. <up-checkbox-group
  98. v-model="model1.checkboxValue1"
  99. shape="square"
  100. @change="change"
  101. >
  102. <up-checkbox
  103. :customStyle="{marginRight: '16px'}"
  104. v-for="(item, index) in checkboxList1"
  105. :key="index"
  106. :label="item.name"
  107. :name="item.name"
  108. >
  109. </up-checkbox>
  110. </up-checkbox-group>
  111. </up-form-item>
  112. <up-form-item
  113. label="简介"
  114. prop="intro"
  115. borderBottom
  116. ref="item3"
  117. >
  118. <up-textarea
  119. placeholder="不低于3个字"
  120. v-model="model1.intro"
  121. count
  122. ></up-textarea>
  123. </up-form-item>
  124. <up-form-item
  125. label="住店时间"
  126. prop="hotel"
  127. labelWidth="80"
  128. borderBottom
  129. @click="showCalendar = true; hideKeyboard()"
  130. >
  131. <up-input
  132. v-model="model1.hotel"
  133. disabled
  134. disabledColor="#ffffff"
  135. placeholder="请选择住店和离店时间"
  136. border="none"
  137. ></up-input>
  138. <template #right>
  139. <up-icon
  140. name="arrow-right"
  141. ></up-icon>
  142. </template>
  143. </up-form-item>
  144. <up-form-item
  145. label="验证码"
  146. prop="code"
  147. labelWidth="80"
  148. borderBottom
  149. >
  150. <up-input
  151. v-model="model1.code"
  152. border="none"
  153. placeholder="请填写验证码"
  154. ></up-input>
  155. <up-button
  156. #right
  157. @tap="getCode"
  158. :text="tips"
  159. type="success"
  160. size="mini"
  161. style="flex: 0.5;"
  162. :disabled="disabled1"
  163. ></up-button>
  164. </up-form-item>
  165. <up-form-item
  166. label="生日"
  167. prop="userInfo.birthday"
  168. borderBottom
  169. @click="showBirthday = true; hideKeyboard()"
  170. ref="item1"
  171. >
  172. <up-input
  173. v-model="model1.userInfo.birthday"
  174. disabled
  175. disabledColor="#ffffff"
  176. placeholder="请选择生日"
  177. border="none"
  178. ></up-input>
  179. <template #right>
  180. <up-icon
  181. name="arrow-right"
  182. ></up-icon>
  183. </template>
  184. </up-form-item>
  185. </up-form>
  186. <up-button
  187. type="primary"
  188. text="提交"
  189. customStyle="margin-top: 50px"
  190. @click="submit"
  191. ></up-button>
  192. <up-button
  193. type="error"
  194. text="重置"
  195. customStyle="margin-top: 10px"
  196. @click="reset"
  197. ></up-button>
  198. <up-action-sheet
  199. :show="showSex"
  200. :actions="actions"
  201. title="请选择性别"
  202. description="如果选择保密会报错"
  203. @close="showSex = false"
  204. @select="sexSelect"
  205. >
  206. </up-action-sheet>
  207. <up-calendar
  208. :show="showCalendar"
  209. mode="range"
  210. @confirm="calendarConfirm"
  211. @close="calendarClose"
  212. startText="住店"
  213. endText="离店"
  214. confirmDisabledText="请选择离店日期"
  215. :formatter="formatter"
  216. ></up-calendar>
  217. <up-code
  218. ref="uCode"
  219. @change="codeChange"
  220. seconds="20"
  221. @start="disabled1 = true"
  222. @end="disabled1 = false"
  223. ></up-code>
  224. <up-datetime-picker
  225. :show="showBirthday"
  226. :value="birthday"
  227. mode="date"
  228. closeOnClickOverlay
  229. @confirm="birthdayConfirm"
  230. @cancel="birthdayClose"
  231. @close="birthdayClose"
  232. ></up-datetime-picker>
  233. </view>
  234. </view>
  235. </view>
  236. </template>
  237. <script setup lang="ts">
  238. import { ref } from 'vue'
  239. import type { UniFormRef } from '@/uni_modules/uview-plus/types'
  240. const form1 = ref<UniFormRef|null>(null)
  241. </script>
  242. <script lang="ts">
  243. export default {
  244. data() {
  245. return {
  246. fileList1: [],
  247. disabled1: false,
  248. tips: '',
  249. value: '',
  250. showCalendar: false,
  251. showBirthday: false,
  252. model1: {
  253. userInfo: {
  254. name: '楼兰',
  255. sex: '',
  256. age: 0,
  257. birthday: ''
  258. },
  259. radiovalue1: '苹果',
  260. checkboxValue1: [],
  261. intro: '',
  262. code: ''
  263. },
  264. showSex: false,
  265. birthday: Number(new Date()),
  266. actions: [{
  267. name: '男',
  268. },
  269. {
  270. name: '女',
  271. },
  272. {
  273. name: '保密',
  274. },
  275. ],
  276. rules: {
  277. 'userInfo.name': [{
  278. type: 'string',
  279. required: true,
  280. message: '请填写姓名',
  281. trigger: ['blur', 'change']
  282. },
  283. {
  284. // 此为同步验证,可以直接返回true或者false,如果是异步验证,稍微不同,见下方说明
  285. validator: (rule, value, callback) => {
  286. // 调用uview-plus自带的js验证规则,详见:https://uview-plus.jiangruyi.com/js/test.html
  287. return uni.$u.test.chinese(value);
  288. },
  289. message: "姓名必须为中文",
  290. // 触发器可以同时用blur和change,二者之间用英文逗号隔开
  291. trigger: ["change", "blur"],
  292. },
  293. {
  294. asyncValidator: (rule, value, callback) => {
  295. setTimeout(() => {
  296. callback('异步规则');
  297. }, 1000)
  298. },
  299. // 触发器可以同时用blur和change,二者之间用英文逗号隔开
  300. trigger: ["change", "blur"],
  301. } ,
  302. ],
  303. code: {
  304. type: 'string',
  305. required: true,
  306. len: 4,
  307. message: '请填写4位验证码',
  308. trigger: ['blur']
  309. },
  310. 'userInfo.sex': {
  311. type: 'string',
  312. max: 1,
  313. required: true,
  314. message: '请选择男或女',
  315. trigger: ['blur', 'change']
  316. },
  317. radiovalue1: {
  318. type: 'string',
  319. min: 1,
  320. max: 2,
  321. message: '橙子有毒',
  322. trigger: ['change']
  323. },
  324. checkboxValue1: {
  325. type: 'array',
  326. min: 2,
  327. required: true,
  328. message: '不能太宅,至少选两项',
  329. trigger: ['change']
  330. },
  331. intro: {
  332. type: 'string',
  333. min: 3,
  334. required: true,
  335. message: '不低于3个字',
  336. trigger: ['change']
  337. },
  338. hotel: {
  339. type: 'string',
  340. min: 2,
  341. required: true,
  342. message: '请选择住店时间',
  343. trigger: ['change']
  344. },
  345. 'userInfo.birthday': {
  346. type: 'string',
  347. required: true,
  348. message: '请选择生日',
  349. trigger: ['change']
  350. },
  351. },
  352. radiolist1: [{
  353. name: '苹果',
  354. disabled: false
  355. },
  356. {
  357. name: '香蕉',
  358. disabled: false
  359. },
  360. {
  361. name: '毒橙子',
  362. disabled: false
  363. }
  364. ],
  365. checkboxList1: [{
  366. name: '羽毛球',
  367. disabled: false
  368. },
  369. {
  370. name: '跑步',
  371. disabled: false
  372. },
  373. {
  374. name: '爬山',
  375. disabled: false
  376. }
  377. ]
  378. }
  379. },
  380. onReady() {
  381. // 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则
  382. this.$refs.form1.setRules(this.rules)
  383. },
  384. methods: {
  385. afterRead(event) {
  386. this.fileList1.push({
  387. url: event.file,
  388. status: 'uploading',
  389. message: '上传中'
  390. })
  391. },
  392. groupChange(n) {
  393. // console.log('groupChange', n);
  394. },
  395. radioChange(n) {
  396. // console.log('radioChange', n);
  397. },
  398. navigateBack() {
  399. uni.navigateBack()
  400. },
  401. sexSelect(e) {
  402. this.model1.userInfo.sex = e.name
  403. this.$refs.form1.validateField('userInfo.sex')
  404. },
  405. change(e) {
  406. // console.log(e);
  407. },
  408. formatter(day) {
  409. const d = new Date()
  410. let month = d.getMonth() + 1
  411. const date = d.getDate()
  412. if (day.month == month && day.day == date + 3) {
  413. day.bottomInfo = '有优惠'
  414. day.dot = true
  415. }
  416. return day
  417. },
  418. calendarConfirm(e) {
  419. this.showCalendar = false
  420. this.model1.hotel = `${e[0]} / ${e[e.length - 1]}`
  421. this.$refs.form1.validateField('hotel')
  422. },
  423. codeChange(text) {
  424. this.tips = text;
  425. },
  426. getCode() {
  427. if (this.$refs.uCode.canGetCode) {
  428. // 模拟向后端请求验证码
  429. uni.showLoading({
  430. title: '正在获取验证码'
  431. })
  432. setTimeout(() => {
  433. uni.hideLoading();
  434. // 这里此提示会被this.start()方法中的提示覆盖
  435. uni.$u.toast('验证码已发送');
  436. // 通知验证码组件内部开始倒计时
  437. this.$refs.uCode.start();
  438. }, 2000);
  439. } else {
  440. uni.$u.toast('倒计时结束后再发送');
  441. }
  442. },
  443. calendarClose() {
  444. this.showCalendar = false
  445. this.$refs.form1.validateField('hotel')
  446. },
  447. birthdayClose() {
  448. this.showBirthday = false
  449. this.$refs.form1.validateField('userInfo.birthday')
  450. },
  451. birthdayConfirm(e) {
  452. this.showBirthday = false
  453. this.model1.userInfo.birthday = uni.$u.timeFormat(e.value, 'yyyy-mm-dd')
  454. this.$refs.form1.validateField('userInfo.birthday')
  455. },
  456. submit() {
  457. // 如果有错误,会在catch中返回报错信息数组,校验通过则在then中返回true
  458. this.$refs.form1.validate().then(res => {
  459. uni.$u.toast('校验通过')
  460. }).catch(errors => {
  461. console.log(errors)
  462. uni.$u.toast('校验失败')
  463. })
  464. },
  465. reset() {
  466. const validateList = ['userInfo.name', 'userInfo.sex', 'radiovalue1', 'checkboxValue1', 'intro',
  467. 'hotel', 'code', 'userInfo.birthday']
  468. this.$refs.form1.resetFields()
  469. this.$refs.form1.clearValidate()
  470. setTimeout(()=>{
  471. this.$refs.form1.clearValidate(validateList)
  472. // 或者使用 this.$refs.form1.clearValidate()
  473. },10)
  474. },
  475. hideKeyboard() {
  476. uni.hideKeyboard()
  477. }
  478. },
  479. }
  480. </script>
  481. <style lang="scss">
  482. </style>