feat: 新增客户表单强制必填校验(名称无空格/手机号11位/押金买断生日)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -29,10 +29,29 @@ const schema = z.object({
|
|||||||
remark: z.string().trim().optional().default(""),
|
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 };
|
export type ActionState = { error?: string };
|
||||||
|
|
||||||
function parse(formData: FormData) {
|
/** 从 FormData 取出待校验的原始字段值 */
|
||||||
return schema.safeParse({
|
function pickForm(formData: FormData) {
|
||||||
|
return {
|
||||||
customerName: formData.get("customerName"),
|
customerName: formData.get("customerName"),
|
||||||
contacts: formData.get("contacts"),
|
contacts: formData.get("contacts"),
|
||||||
balanceUser: formData.get("balanceUser"),
|
balanceUser: formData.get("balanceUser"),
|
||||||
@@ -45,7 +64,7 @@ function parse(formData: FormData) {
|
|||||||
birthdayType: formData.get("birthdayType") ?? 1,
|
birthdayType: formData.get("birthdayType") ?? 1,
|
||||||
birthday: formData.get("birthday") ?? "",
|
birthday: formData.get("birthday") ?? "",
|
||||||
remark: formData.get("remark") ?? "",
|
remark: formData.get("remark") ?? "",
|
||||||
});
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 把校验后的表单值转成 Prisma 写入数据(日期字段转 Date) */
|
/** 把校验后的表单值转成 Prisma 写入数据(日期字段转 Date) */
|
||||||
@@ -63,7 +82,7 @@ export async function createCustomer(
|
|||||||
formData: FormData
|
formData: FormData
|
||||||
): Promise<ActionState> {
|
): Promise<ActionState> {
|
||||||
if (!(await auth())) return { error: "未登录" };
|
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 };
|
if (!r.success) return { error: r.error.issues[0].message };
|
||||||
|
|
||||||
await prisma.customer.create({ data: toData(r.data) });
|
await prisma.customer.create({ data: toData(r.data) });
|
||||||
@@ -77,7 +96,7 @@ export async function updateCustomer(
|
|||||||
formData: FormData
|
formData: FormData
|
||||||
): Promise<ActionState> {
|
): Promise<ActionState> {
|
||||||
if (!(await auth())) return { error: "未登录" };
|
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 };
|
if (!r.success) return { error: r.error.issues[0].message };
|
||||||
|
|
||||||
await prisma.customer.update({ where: { id }, data: toData(r.data) });
|
await prisma.customer.update({ where: { id }, data: toData(r.data) });
|
||||||
|
|||||||
@@ -29,12 +29,18 @@ function Field({
|
|||||||
defaultValue,
|
defaultValue,
|
||||||
required,
|
required,
|
||||||
type = "text",
|
type = "text",
|
||||||
|
pattern,
|
||||||
|
inputMode,
|
||||||
|
title,
|
||||||
}: {
|
}: {
|
||||||
label: string;
|
label: string;
|
||||||
name: string;
|
name: string;
|
||||||
defaultValue?: string | null;
|
defaultValue?: string | null;
|
||||||
required?: boolean;
|
required?: boolean;
|
||||||
type?: string;
|
type?: string;
|
||||||
|
pattern?: string;
|
||||||
|
inputMode?: React.HTMLAttributes<HTMLInputElement>["inputMode"];
|
||||||
|
title?: string;
|
||||||
}) {
|
}) {
|
||||||
return (
|
return (
|
||||||
<div className="space-y-1.5">
|
<div className="space-y-1.5">
|
||||||
@@ -42,7 +48,16 @@ function Field({
|
|||||||
{label}
|
{label}
|
||||||
{required && <span className="ml-0.5 text-[var(--color-danger)]">*</span>}
|
{required && <span className="ml-0.5 text-[var(--color-danger)]">*</span>}
|
||||||
</Label>
|
</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>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -51,10 +66,12 @@ export function CustomerForm({
|
|||||||
action,
|
action,
|
||||||
defaultValues,
|
defaultValues,
|
||||||
submitLabel = "保存",
|
submitLabel = "保存",
|
||||||
|
requireAll = false,
|
||||||
}: {
|
}: {
|
||||||
action: (prev: ActionState, formData: FormData) => Promise<ActionState>;
|
action: (prev: ActionState, formData: FormData) => Promise<ActionState>;
|
||||||
defaultValues?: CustomerValues;
|
defaultValues?: CustomerValues;
|
||||||
submitLabel?: string;
|
submitLabel?: string;
|
||||||
|
requireAll?: boolean;
|
||||||
}) {
|
}) {
|
||||||
const [state, formAction, pending] = useActionState(action, {});
|
const [state, formAction, pending] = useActionState(action, {});
|
||||||
const v = defaultValues ?? {};
|
const v = defaultValues ?? {};
|
||||||
@@ -62,11 +79,26 @@ export function CustomerForm({
|
|||||||
return (
|
return (
|
||||||
<form action={formAction} className="space-y-6">
|
<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">
|
<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="contacts" defaultValue={v.contacts} required />
|
||||||
<Field label="结算人" name="balanceUser" defaultValue={v.balanceUser} required />
|
<Field label="结算人" name="balanceUser" defaultValue={v.balanceUser} required />
|
||||||
<Field label="业务员" name="salesman" defaultValue={v.salesman} 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
|
<Field
|
||||||
label="报单时间"
|
label="报单时间"
|
||||||
name="reportDate"
|
name="reportDate"
|
||||||
@@ -74,8 +106,8 @@ export function CustomerForm({
|
|||||||
defaultValue={fmtDate(v.reportDate)}
|
defaultValue={fmtDate(v.reportDate)}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
<Field label="押金" name="deposit" defaultValue={v.deposit} />
|
<Field label="押金" name="deposit" defaultValue={v.deposit} required={requireAll} />
|
||||||
<Field label="买断" name="buy" defaultValue={v.buy} />
|
<Field label="买断" name="buy" defaultValue={v.buy} required={requireAll} />
|
||||||
<div className="space-y-1.5">
|
<div className="space-y-1.5">
|
||||||
<Label htmlFor="birthdayType">生日类型</Label>
|
<Label htmlFor="birthdayType">生日类型</Label>
|
||||||
<Select id="birthdayType" name="birthdayType" defaultValue={String(v.birthdayType ?? 1)}>
|
<Select id="birthdayType" name="birthdayType" defaultValue={String(v.birthdayType ?? 1)}>
|
||||||
@@ -83,7 +115,13 @@ export function CustomerForm({
|
|||||||
<option value="2">阴历</option>
|
<option value="2">阴历</option>
|
||||||
</Select>
|
</Select>
|
||||||
</div>
|
</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>
|
||||||
|
|
||||||
<div className="space-y-1.5">
|
<div className="space-y-1.5">
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export default function NewCustomerPage() {
|
|||||||
<CardTitle>新增客户</CardTitle>
|
<CardTitle>新增客户</CardTitle>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
<CustomerForm action={createCustomer} submitLabel="创建" />
|
<CustomerForm action={createCustomer} submitLabel="创建" requireAll />
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user