diff --git a/src/app/(dashboard)/customers/actions.ts b/src/app/(dashboard)/customers/actions.ts index d1a29a2..04fbf05 100644 --- a/src/app/(dashboard)/customers/actions.ts +++ b/src/app/(dashboard)/customers/actions.ts @@ -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 { 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 { 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) }); diff --git a/src/app/(dashboard)/customers/customer-form.tsx b/src/app/(dashboard)/customers/customer-form.tsx index 8246f7c..373c7fd 100644 --- a/src/app/(dashboard)/customers/customer-form.tsx +++ b/src/app/(dashboard)/customers/customer-form.tsx @@ -29,12 +29,18 @@ function Field({ defaultValue, required, type = "text", + pattern, + inputMode, + title, }: { label: string; name: string; defaultValue?: string | null; required?: boolean; type?: string; + pattern?: string; + inputMode?: React.HTMLAttributes["inputMode"]; + title?: string; }) { return (
@@ -42,7 +48,16 @@ function Field({ {label} {required && *} - +
); } @@ -51,10 +66,12 @@ export function CustomerForm({ action, defaultValues, submitLabel = "保存", + requireAll = false, }: { action: (prev: ActionState, formData: FormData) => Promise; defaultValues?: CustomerValues; submitLabel?: string; + requireAll?: boolean; }) { const [state, formAction, pending] = useActionState(action, {}); const v = defaultValues ?? {}; @@ -62,11 +79,26 @@ export function CustomerForm({ return (
- + - + - - + +
- +
diff --git a/src/app/(dashboard)/customers/new/page.tsx b/src/app/(dashboard)/customers/new/page.tsx index be11cb7..93884ab 100644 --- a/src/app/(dashboard)/customers/new/page.tsx +++ b/src/app/(dashboard)/customers/new/page.tsx @@ -18,7 +18,7 @@ export default function NewCustomerPage() { 新增客户 - +