feat: 封装手机号校验,mobile 非必填校验,mobileRequired 必填校验

This commit is contained in:
xingyu4j
2025-04-23 17:51:43 +08:00
parent 26c2638146
commit c867f93e0a
6 changed files with 42 additions and 8 deletions

View File

@@ -8,6 +8,9 @@ import type { ComponentType } from './component';
import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui';
import { $t } from '@vben/locales';
/** 手机号正则表达式(中国) */
const MOBILE_REGEX = /(?:0|86|\+86)?1[3-9]\d{9}/;
setupVbenForm<ComponentType>({
config: {
// ant design vue组件库默认都是 v-model:value
@@ -37,6 +40,25 @@ setupVbenForm<ComponentType>({
}
return true;
},
// 手机号非必填
mobile: (value, _params, ctx) => {
if (value === undefined || value === null || value.length === 0) {
return true;
} else if (!MOBILE_REGEX.test(value)) {
return $t('ui.formRules.phone', [ctx.label]);
}
return true;
},
// 手机号必填
mobileRequired: (value, _params, ctx) => {
if (value === undefined || value === null || value.length === 0) {
return $t('ui.formRules.required', [ctx.label]);
}
if (!MOBILE_REGEX.test(value)) {
return $t('ui.formRules.phone', [ctx.label]);
}
return true;
},
},
});

View File

@@ -92,11 +92,7 @@ export function useFormSchema(): VbenFormSchema[] {
maxLength: 11,
placeholder: '请输入联系电话',
},
rules: z
.string()
// TODO @芋艿:未来怎么拓展一个手机的
.regex(/^1[3-9|]\d{9}$/, '请输入正确的手机号码')
.optional(),
rules: 'mobileRequired',
},
{
fieldName: 'email',