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) });
@@ -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<HTMLInputElement>["inputMode"];
title?: string;
}) {
return (
<div className="space-y-1.5">
@@ -42,7 +48,16 @@ function Field({
{label}
{required && <span className="ml-0.5 text-[var(--color-danger)]">*</span>}
</Label>
<Input id={name} name={name} type={type} defaultValue={defaultValue ?? ""} />
<Input
id={name}
name={name}
type={type}
defaultValue={defaultValue ?? ""}
required={required}
pattern={pattern}
inputMode={inputMode}
title={title}
/>
</div>
);
}
@@ -51,10 +66,12 @@ export function CustomerForm({
action,
defaultValues,
submitLabel = "保存",
requireAll = false,
}: {
action: (prev: ActionState, formData: FormData) => Promise<ActionState>;
defaultValues?: CustomerValues;
submitLabel?: string;
requireAll?: boolean;
}) {
const [state, formAction, pending] = useActionState(action, {});
const v = defaultValues ?? {};
@@ -62,11 +79,26 @@ export function CustomerForm({
return (
<form action={formAction} className="space-y-6">
<div className="grid grid-cols-1 gap-x-5 gap-y-4 sm:grid-cols-2 lg:grid-cols-3">
<Field label="客户名称" name="customerName" defaultValue={v.customerName} required />
<Field
label="客户名称"
name="customerName"
defaultValue={v.customerName}
required
pattern={requireAll ? "\\S+" : undefined}
title={requireAll ? "客户名称不能包含空格" : undefined}
/>
<Field label="联系人" name="contacts" defaultValue={v.contacts} required />
<Field label="结算人" name="balanceUser" defaultValue={v.balanceUser} required />
<Field label="业务员" name="salesman" defaultValue={v.salesman} required />
<Field label="电话" name="phone" defaultValue={v.phone} />
<Field
label="电话"
name="phone"
defaultValue={v.phone}
required={requireAll}
pattern={requireAll ? "1\\d{10}" : undefined}
inputMode={requireAll ? "numeric" : undefined}
title={requireAll ? "请输入 11 位手机号" : undefined}
/>
<Field
label="报单时间"
name="reportDate"
@@ -74,8 +106,8 @@ export function CustomerForm({
defaultValue={fmtDate(v.reportDate)}
required
/>
<Field label="押金" name="deposit" defaultValue={v.deposit} />
<Field label="买断" name="buy" defaultValue={v.buy} />
<Field label="押金" name="deposit" defaultValue={v.deposit} required={requireAll} />
<Field label="买断" name="buy" defaultValue={v.buy} required={requireAll} />
<div className="space-y-1.5">
<Label htmlFor="birthdayType">生日类型</Label>
<Select id="birthdayType" name="birthdayType" defaultValue={String(v.birthdayType ?? 1)}>
@@ -83,7 +115,13 @@ export function CustomerForm({
<option value="2">阴历</option>
</Select>
</div>
<Field label="生日" name="birthday" type="date" defaultValue={fmtDate(v.birthday)} />
<Field
label="生日"
name="birthday"
type="date"
defaultValue={fmtDate(v.birthday)}
required={requireAll}
/>
</div>
<div className="space-y-1.5">
+1 -1
View File
@@ -18,7 +18,7 @@ export default function NewCustomerPage() {
<CardTitle>新增客户</CardTitle>
</CardHeader>
<CardContent>
<CustomerForm action={createCustomer} submitLabel="创建" />
<CustomerForm action={createCustomer} submitLabel="创建" requireAll />
</CardContent>
</Card>
</div>