提交 | 用户 | 时间
|
820397
|
1 |
import { useI18n } from '@/hooks/web/useI18n' |
H |
2 |
import { FormItemRule } from 'element-plus' |
|
3 |
|
|
4 |
const { t } = useI18n() |
|
5 |
|
|
6 |
interface LengthRange { |
|
7 |
min: number |
|
8 |
max: number |
|
9 |
message?: string |
|
10 |
} |
|
11 |
|
|
12 |
export const useValidator = () => { |
|
13 |
const required = (message?: string): FormItemRule => { |
|
14 |
return { |
|
15 |
required: true, |
|
16 |
message: message || t('common.required') |
|
17 |
} |
|
18 |
} |
|
19 |
|
|
20 |
const lengthRange = (options: LengthRange): FormItemRule => { |
|
21 |
const { min, max, message } = options |
|
22 |
|
|
23 |
return { |
|
24 |
min, |
|
25 |
max, |
|
26 |
message: message || t('common.lengthRange', { min, max }) |
|
27 |
} |
|
28 |
} |
|
29 |
|
|
30 |
const notSpace = (message?: string): FormItemRule => { |
|
31 |
return { |
|
32 |
validator: (_, val, callback) => { |
|
33 |
if (val?.indexOf(' ') !== -1) { |
|
34 |
callback(new Error(message || t('common.notSpace'))) |
|
35 |
} else { |
|
36 |
callback() |
|
37 |
} |
|
38 |
} |
|
39 |
} |
|
40 |
} |
|
41 |
|
|
42 |
const notSpecialCharacters = (message?: string): FormItemRule => { |
|
43 |
return { |
|
44 |
validator: (_, val, callback) => { |
|
45 |
if (/[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/gi.test(val)) { |
|
46 |
callback(new Error(message || t('common.notSpecialCharacters'))) |
|
47 |
} else { |
|
48 |
callback() |
|
49 |
} |
|
50 |
} |
|
51 |
} |
|
52 |
} |
|
53 |
|
|
54 |
return { |
|
55 |
required, |
|
56 |
lengthRange, |
|
57 |
notSpace, |
|
58 |
notSpecialCharacters |
|
59 |
} |
|
60 |
} |