forms.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <template>
  2. <view>
  3. <text class="example-info"> uni-forms 组件一般由输入框、选择器、单选框、多选框等控件组成,用以收集、校验、提交数据。</text>
  4. <uni-section title="基础用法" type="line"></uni-section>
  5. <uni-forms ref="form" labelPosition="left" labelAlign="left" :form-rules="rules" @submit="submit" @reset="reset" @validate="validate">
  6. <uni-forms-item name="name" label="用户名">
  7. <input type="text" placeholder="请输入用户名" @blur="input('form','name',$event.detail.value)">
  8. </uni-forms-item>
  9. <uni-forms-item name="age" label="年龄">
  10. <input type="text" placeholder="请输入年龄" @input="input('form','age',$event.detail.value)">
  11. </uni-forms-item>
  12. <uni-forms-item name="email" label="邮箱">
  13. <input type="text" placeholder="请输入邮箱" @blur="input('form','email',$event.detail.value)">
  14. </uni-forms-item>
  15. <!-- 直接使用组件自带submit、reset 方法,小程序不生效 -->
  16. <!-- <button class="button" form-type="submit">Submit</button>
  17. <button class="button" form-type="reset">Reset</button> -->
  18. <button class="button" @click="submitForm('form')">校验表单</button>
  19. <button class="button" @click="validateField('form')">校验部分表单</button>
  20. <button class="button" @click="clearValidate('form','name')">移除部分表单校验结果</button>
  21. <button class="button" @click="clearValidate('form')">移除全部表单校验结果</button>
  22. </uni-forms>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. data() {
  28. return {
  29. formData: {
  30. name: '',
  31. age: '',
  32. email: '',
  33. size: ''
  34. },
  35. rules: {
  36. name: {
  37. rules: [{
  38. required: true,
  39. errorMessage: '请输入姓名',
  40. trigger: 'blur'
  41. }, {
  42. minLength: 3,
  43. maxLength: 5,
  44. errorMessage: '姓名长度在 {minLength} 到 {maxLength} 个字符',
  45. trigger: 'blur'
  46. }]
  47. },
  48. age: {
  49. rules: [{
  50. required: true,
  51. errorMessage: '请填写年龄',
  52. trigger: "blur"
  53. }, {
  54. format: 'number',
  55. errorMessage: '年龄必须是数字',
  56. // trigger: 'blur'
  57. }, {
  58. minimum: 18,
  59. maximum: 30,
  60. errorMessage: '年龄应该大于 {minimum} 岁,小于 {maximum} 岁',
  61. trigger: 'change'
  62. }]
  63. },
  64. email: {
  65. rules: [{
  66. format: 'email',
  67. errorMessage: '请输入正确的邮箱地址',
  68. trigger: 'blur'
  69. }]
  70. }
  71. }
  72. }
  73. },
  74. methods: {
  75. input(form, name, value) {
  76. this.$refs[form].setValue(name, value)
  77. },
  78. /**
  79. * 触发校验
  80. * @param {Object} event
  81. */
  82. validate(event) {
  83. console.log('触发校验:', event);
  84. },
  85. /**
  86. * 表单提交
  87. * @param {Object} event
  88. */
  89. submit(event) {
  90. console.log(event)
  91. },
  92. /**
  93. * 手动提交
  94. * @param {Object} event
  95. */
  96. reset(event) {
  97. console.log('表单重置:', event);
  98. },
  99. /**
  100. * 手动提交
  101. * @param {Object} form
  102. */
  103. submitForm(form) {
  104. this.$refs[form].validate((valid, rules) => {
  105. if (valid) {
  106. console.log('校验通过')
  107. } else {
  108. console.error('校验失败', rules);
  109. }
  110. })
  111. },
  112. /**
  113. * 部分表单校验
  114. * @param {Object} form
  115. */
  116. validateField(form) {
  117. this.$refs[form].validateField(['name', 'email'], (error) => {
  118. console.log(error);
  119. })
  120. },
  121. /**
  122. * 清除表单校验
  123. * @param {Object} form
  124. * @param {Object} name
  125. */
  126. clearValidate(form, name) {
  127. if (!name) name = []
  128. this.$refs[form].clearValidate(name)
  129. }
  130. }
  131. }
  132. </script>
  133. <style>
  134. @charset "UTF-8";
  135. /* 头条小程序组件内不能引入字体 */
  136. /* #ifdef MP-TOUTIAO */
  137. @font-face {
  138. font-family: uniicons;
  139. font-weight: normal;
  140. font-style: normal;
  141. src: url("~@/static/uni.ttf") format("truetype");
  142. }
  143. /* #endif */
  144. /* #ifndef APP-NVUE */
  145. page {
  146. display: flex;
  147. flex-direction: column;
  148. box-sizing: border-box;
  149. background-color: #efeff4;
  150. min-height: 100%;
  151. height: auto;
  152. }
  153. view {
  154. font-size: 14px;
  155. line-height: inherit;
  156. }
  157. .example {
  158. padding: 0 15px 15px;
  159. }
  160. .example-info {
  161. padding: 15px;
  162. color: #3b4144;
  163. background: #ffffff;
  164. }
  165. .example-body {
  166. /* #ifndef APP-NVUE */
  167. display: flex;
  168. /* #endif */
  169. flex-direction: row;
  170. flex-wrap: wrap;
  171. justify-content: center;
  172. padding: 0;
  173. font-size: 14px;
  174. background-color: #ffffff;
  175. }
  176. /* #endif */
  177. .example {
  178. padding: 0 15px;
  179. }
  180. .example-info {
  181. /* #ifndef APP-NVUE */
  182. display: block;
  183. /* #endif */
  184. padding: 15px;
  185. color: #3b4144;
  186. background-color: #ffffff;
  187. font-size: 14px;
  188. line-height: 20px;
  189. }
  190. .example-info-text {
  191. font-size: 14px;
  192. line-height: 20px;
  193. color: #3b4144;
  194. }
  195. .example-body {
  196. flex-direction: column;
  197. padding: 15px;
  198. background-color: #ffffff;
  199. }
  200. .word-btn-white {
  201. font-size: 18px;
  202. color: #FFFFFF;
  203. }
  204. .word-btn {
  205. /* #ifndef APP-NVUE */
  206. display: flex;
  207. /* #endif */
  208. flex-direction: row;
  209. align-items: center;
  210. justify-content: center;
  211. border-radius: 6px;
  212. height: 48px;
  213. margin: 15px;
  214. background-color: #007AFF;
  215. }
  216. .word-btn--hover {
  217. background-color: #4ca2ff;
  218. }
  219. .button {
  220. margin: 10px 15px;
  221. }
  222. </style>