RuleForm.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <template>
  2. <PageWrapper title="表单校验示例">
  3. <div class="mb-4">
  4. <a-button @click="validateForm" class="mr-2"> 手动校验表单</a-button>
  5. <a-button @click="resetValidate" class="mr-2"> 清空校验信息</a-button>
  6. <a-button @click="getFormValues" class="mr-2"> 获取表单值</a-button>
  7. <a-button @click="setFormValues" class="mr-2"> 设置表单值</a-button>
  8. <a-button @click="resetFields" class="mr-2"> 重置</a-button>
  9. </div>
  10. <CollapseContainer title="表单校验">
  11. <BasicForm @register="register" @submit="handleSubmit" />
  12. </CollapseContainer>
  13. </PageWrapper>
  14. </template>
  15. <script lang="ts">
  16. import { defineComponent } from 'vue';
  17. import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  18. import { CollapseContainer } from '/@/components/Container';
  19. import { useMessage } from '/@/hooks/web/useMessage';
  20. import { PageWrapper } from '/@/components/Page';
  21. import { isAccountExist } from '/@/api/demo/system';
  22. const schemas: FormSchema[] = [
  23. {
  24. field: 'field1',
  25. component: 'Input',
  26. label: '字段1',
  27. colProps: {
  28. span: 8,
  29. },
  30. required: true,
  31. },
  32. {
  33. field: 'field2',
  34. component: 'Input',
  35. label: '字段2',
  36. colProps: {
  37. span: 8,
  38. },
  39. required: true,
  40. },
  41. {
  42. field: 'id',
  43. label: 'id',
  44. required: true,
  45. defaultValue: 0,
  46. component: 'InputNumber',
  47. show: false,
  48. },
  49. {
  50. field: 'field3',
  51. component: 'DatePicker',
  52. label: '字段3',
  53. colProps: {
  54. span: 8,
  55. },
  56. required: true,
  57. },
  58. {
  59. field: 'field33',
  60. component: 'DatePicker',
  61. label: '字段33',
  62. colProps: {
  63. span: 8,
  64. },
  65. componentProps: {
  66. valueFormat: 'YYYY-MM-DD',
  67. },
  68. rules: [{ required: true, type: 'string' }],
  69. },
  70. {
  71. field: 'field44',
  72. component: 'InputCountDown',
  73. label: '验证码',
  74. colProps: {
  75. span: 8,
  76. },
  77. required: true,
  78. },
  79. {
  80. field: 'field4',
  81. component: 'Select',
  82. label: '字段4',
  83. colProps: {
  84. span: 8,
  85. },
  86. componentProps: {
  87. mode: 'multiple',
  88. options: [
  89. {
  90. label: '选项1',
  91. value: '1',
  92. key: '1',
  93. },
  94. {
  95. label: '选项2',
  96. value: '2',
  97. key: '2',
  98. },
  99. ],
  100. },
  101. rules: [
  102. {
  103. required: true,
  104. message: '请输入aa',
  105. type: 'array',
  106. },
  107. ],
  108. },
  109. {
  110. field: 'field441',
  111. component: 'Input',
  112. label: '自定义校验',
  113. colProps: {
  114. span: 8,
  115. },
  116. rules: [
  117. {
  118. required: true,
  119. // @ts-ignore
  120. validator: async (rule, value) => {
  121. if (!value) {
  122. /* eslint-disable-next-line */
  123. return Promise.reject('值不能为空');
  124. }
  125. if (value === '1') {
  126. /* eslint-disable-next-line */
  127. return Promise.reject('值不能为1');
  128. }
  129. return Promise.resolve();
  130. },
  131. trigger: 'change',
  132. },
  133. ],
  134. },
  135. {
  136. field: 'field5',
  137. component: 'CheckboxGroup',
  138. label: '字段5',
  139. colProps: {
  140. span: 8,
  141. },
  142. componentProps: {
  143. options: [
  144. {
  145. label: '选项1',
  146. value: '1',
  147. },
  148. {
  149. label: '选项2',
  150. value: '2',
  151. },
  152. ],
  153. },
  154. rules: [{ required: true }],
  155. },
  156. {
  157. field: 'field7',
  158. component: 'RadioGroup',
  159. label: '字段7',
  160. colProps: {
  161. span: 8,
  162. },
  163. componentProps: {
  164. options: [
  165. {
  166. label: '选项1',
  167. value: '1',
  168. },
  169. {
  170. label: '选项2',
  171. value: '2',
  172. },
  173. ],
  174. },
  175. rules: [{ required: true, message: '覆盖默认生成的校验信息' }],
  176. },
  177. {
  178. field: 'field8',
  179. component: 'Input',
  180. label: '后端异步验证',
  181. colProps: {
  182. span: 8,
  183. },
  184. helpMessage: ['本字段演示异步验证', '本地规则:必须填写', '后端规则:不能包含admin'],
  185. rules: [
  186. {
  187. required: true,
  188. message: '请输入数据',
  189. },
  190. {
  191. validator(_, value) {
  192. return new Promise((resolve, reject) => {
  193. isAccountExist(value)
  194. .then(() => resolve())
  195. .catch((err) => {
  196. reject(err.message || '验证失败');
  197. });
  198. });
  199. },
  200. },
  201. ],
  202. },
  203. ];
  204. export default defineComponent({
  205. components: { BasicForm, CollapseContainer, PageWrapper },
  206. setup() {
  207. const { createMessage } = useMessage();
  208. const [register, { validateFields, clearValidate, getFieldsValue, resetFields, setFieldsValue }] = useForm({
  209. labelWidth: 120,
  210. schemas,
  211. actionColOptions: {
  212. span: 24,
  213. },
  214. });
  215. async function validateForm() {
  216. try {
  217. const res = await validateFields();
  218. console.log('passing', res);
  219. } catch (error) {
  220. console.log('not passing', error);
  221. }
  222. }
  223. async function resetValidate() {
  224. clearValidate();
  225. }
  226. function getFormValues() {
  227. const values = getFieldsValue();
  228. createMessage.success('values:' + JSON.stringify(values));
  229. }
  230. function setFormValues() {
  231. setFieldsValue({
  232. field1: 1111,
  233. field5: ['1'],
  234. field7: '1',
  235. field33: '2020-12-12',
  236. field3: '2020-12-12',
  237. });
  238. }
  239. return {
  240. register,
  241. schemas,
  242. handleSubmit: (values: any) => {
  243. createMessage.success('click search,values:' + JSON.stringify(values));
  244. },
  245. getFormValues,
  246. setFormValues,
  247. validateForm,
  248. resetValidate,
  249. resetFields,
  250. };
  251. },
  252. });
  253. </script>