feat: 新增客户表单强制必填校验(名称无空格/手机号11位/押金买断生日)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-11 21:17:59 +08:00
co-authored by Claude Opus 4.8
parent 1e8396d0d3
commit 228c37c897
3 changed files with 69 additions and 12 deletions
+24 -5
View File
@@ -29,10 +29,29 @@ const schema = z.object({
remark: z.string().trim().optional().default(""),
});
// 新增客户更严格:名称无空格、手机号必填 11 位、押金/买断/生日必填(仅用于创建,编辑仍用 schema)
const createSchema = schema.extend({
customerName: z
.string()
.trim()
.min(1, "客户名称必填")
.max(500)
.refine((s) => !/\s/.test(s), "客户名称不能包含空格"),
phone: z.string().trim().regex(/^1\d{10}$/, "手机号必须为 11 位数字"),
deposit: z.string().trim().min(1, "押金必填"),
buy: z.string().trim().min(1, "买断必填"),
birthday: z
.string()
.trim()
.min(1, "生日必填")
.refine((s) => parseDate(s) !== null, "生日格式应为 YYYY-MM-DD"),
});
export type ActionState = { error?: string };
function parse(formData: FormData) {
return schema.safeParse({
/** 从 FormData 取出待校验的原始字段值 */
function pickForm(formData: FormData) {
return {
customerName: formData.get("customerName"),
contacts: formData.get("contacts"),
balanceUser: formData.get("balanceUser"),
@@ -45,7 +64,7 @@ function parse(formData: FormData) {
birthdayType: formData.get("birthdayType") ?? 1,
birthday: formData.get("birthday") ?? "",
remark: formData.get("remark") ?? "",
});
};
}
/** 把校验后的表单值转成 Prisma 写入数据(日期字段转 Date) */
@@ -63,7 +82,7 @@ export async function createCustomer(
formData: FormData
): Promise<ActionState> {
if (!(await auth())) return { error: "未登录" };
const r = parse(formData);
const r = createSchema.safeParse(pickForm(formData));
if (!r.success) return { error: r.error.issues[0].message };
await prisma.customer.create({ data: toData(r.data) });
@@ -77,7 +96,7 @@ export async function updateCustomer(
formData: FormData
): Promise<ActionState> {
if (!(await auth())) return { error: "未登录" };
const r = parse(formData);
const r = schema.safeParse(pickForm(formData));
if (!r.success) return { error: r.error.issues[0].message };
await prisma.customer.update({ where: { id }, data: toData(r.data) });