rules.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. export const simpleRequired = (message = '请输入') => {
  2. return {
  3. required: true,
  4. message,
  5. transform(value) {
  6. if(null === value || undefined === value || '' === value) return value
  7. return value + '';
  8. },
  9. trigger: ['blur', 'change'],
  10. }
  11. }
  12. export const arrayRequired = (message = '必须项') => {
  13. return {
  14. required: true,
  15. message,
  16. transform(arr) {
  17. console.log('rrrrr',arr);
  18. if(null === arr || undefined === arr || '' === arr) return arr
  19. if(arr.length <= 0) return null
  20. return arr.toString()
  21. },
  22. trigger: ['blur', 'change'],
  23. }
  24. }
  25. export const defaultRequired = {
  26. required: true,
  27. message: '请输入',
  28. transform(value) {
  29. if(null === value || undefined === value || '' === value) return value
  30. return value + '';
  31. },
  32. trigger: ['blur', 'change'],
  33. }
  34. export const carNoRule = {
  35. min: 5,
  36. max: 8,
  37. message: '请输入正确的车牌号',
  38. }
  39. export const mobileRule = {
  40. pattern: /^1[3-9]\d{9}$/,
  41. transform(value) {
  42. return String(value);
  43. },
  44. message: '请输入正确的手机号',
  45. }
  46. export const idRule = {
  47. pattern: /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X|x)$/,
  48. transform(value) {
  49. return String(value);
  50. },
  51. message: '请输入正确的证件号',
  52. }
  53. export const mkRules = (fields, fun = simpleRequired) => {
  54. const ret = {}
  55. fields.forEach(d => {
  56. ret[d] = [fun()]
  57. })
  58. return ret
  59. }