Skip to Content

Form

Form nhập và sửa bản ghi: chọn giữa DrawerForm, ModalForm, StepsForm và ProForm trên trang, định dạng tiền và ngày kiểu Việt Nam, nhập dòng hàng và hiện lỗi server.

Form của pro-components tự lo nút gửi, trạng thái đang gửi, đóng form khi xong và kiểm field bắt buộc. Bạn khai báo field, gọi API trong onFinish rồi trả true để đóng form. Ví dụ dưới đây là form tạo đề xuất mua có số tiền, hạng mục và ngày cần hàng.

Form trong DrawerForm tạo/sửa trong DrawerForm: ProFormMoney (₫), nhóm field, tooltip, chặn ngày quá khứ, trả true để đóng.
ví-dụ.manh.run · dữ liệu mẫuMở để sửa ↗
Ví dụ chạy khi bạn cuộn tới

Chọn kiểu form

  • DrawerForm: tạo hoặc sửa một bản ghi từ danh sách, khi form có từ 6 field trở lên hoặc có dòng hàng.
  • ModalForm: form tối đa 4–5 field, hoặc một quyết định như duyệt, từ chối kèm ghi chú.
  • StepsForm: luồng dài chia thành nhiều bước, ví dụ điền → xác nhận → kết quả.
  • ProForm trên trang: form dài như cài đặt, hồ sơ, chứng từ nhiều phần. Nút gửi đặt ở FooterToolbar.

Không mở trang riêng chỉ để tạo một bản ghi từ danh sách.

Form tạo và sửa trong Drawer

Dùng một component cho cả nút Tạo yêu cầu ở danh sách lẫn nút Sửa ở trang chi tiết. Khi initial có giá trị, form ở chế độ sửa.

app/requests/request-form.tsx
<DrawerForm title={title} trigger={trigger} width={560} drawerProps={{ destroyOnHidden: true }} submitter={{ searchConfig: { submitText: initial ? "Lưu thay đổi" : "Gửi đề xuất", resetText: "Huỷ" } }} initialValues={initial ?? { category: "office" }} onFinish={async (v) => { await onSubmit(v); return true; }} // trả true thì Drawer đóng > <ProFormText name="title" label="Tiêu đề" tooltip="Viết đủ để quản lý hiểu không cần mở chi tiết" rules={[{ required: true, min: 3, max: 200, message: "3–200 ký tự" }]} /> <ProFormGroup> <ProFormMoney name="amount" label="Số tiền" width="sm" customSymbol="₫" locale="vi-VN" min={1} fieldProps={{ precision: 0 }} rules={[{ required: true, message: "Nhập số tiền lớn hơn 0" }]} /> <ProFormSelect name="category" label="Hạng mục" width="sm" valueEnum={CATEGORY_ENUM} rules={[{ required: true }]} /> </ProFormGroup> <ProFormTextArea name="description" label="Mô tả" fieldProps={{ rows: 5, maxLength: 2000, showCount: true }} /> </DrawerForm>

Ở trang danh sách, gửi xong thì gọi actionRef.current?.reload() để bảng tải lại.

Ngày hiện DD/MM/YYYY, gửi YYYY-MM-DD

Field ngày có fieldProps.format gửi đi đúng chuỗi đang hiện (“20/11/2026”), nên API từ chối. Đổi lại định dạng ngay trên field bằng transform:

app/requests/request-form.tsx
<ProFormDatePicker name="neededBy" label="Cần trước ngày" width="sm" fieldProps={{ format: "DD/MM/YYYY", disabledDate: (d) => d.isBefore(dayjs().startOf("day")) }} transform={(v) => ({ neededBy: v ? v.split("/").reverse().join("-") : undefined })} />

Hoặc bỏ format để form gửi YYYY-MM-DD.

Độ rộng và nhóm field

  • width="xs" | "sm" | "md" | "lg" | "xl" (104 / 216 / 328 / 440 / 552 px) để các field rộng đều nhau.
  • Cần cột co giãn theo màn hình: grid trên form + colProps={{ xl: 12 }} trên field.
  • Không trộn width với fieldProps.style.width.
  • Nhiều field ngắn trên một hàng: bọc trong ProFormGroup.

Field phụ thuộc field khác

Hiện field theo giá trị field khác bằng ProFormDependency:

app/orders/order-form.tsx
<ProFormRadio.Group name="delivery" label="Nhận hàng" options={[ { label: "Giao tận nơi", value: "ship" }, { label: "Lấy tại cửa hàng", value: "pickup" }, ]} /> <ProFormDependency name={["delivery"]}> {({ delivery }) => delivery === "ship" ? <ProFormText name="address" label="Địa chỉ giao" rules={[{ required: true }]} /> : <ProFormSelect name="storeId" label="Cửa hàng" request={loadStores} showSearch />} </ProFormDependency>

Dòng hàng trong form

Đặt name cho EditableProTable thì bảng trở thành một field, và onFinish nhận values.lines. Bỏ key tạm trước khi gửi. Server tự tính giá và tổng.

app/orders/order-form.tsx
const [keys, setKeys] = useState<React.Key[]>([]); <EditableProTable<Line> name="lines" rowKey="key" columns={lineColumns} // cột tiền chỉ đọc recordCreatorProps={{ position: "bottom", record: () => ({ key: Date.now() }), creatorButtonText: "Thêm dòng" }} editable={{ type: "multiple", editableKeys: keys, onChange: setKeys }} /> // onFinish: lines.map(({ key, ...l }) => l)

Dòng có 1–2 field thì dùng ProFormList cho gọn.

Form dài trên trang

Form cài đặt hoặc chứng từ nhiều phần đặt thẳng trên trang. Mỗi phần một ProCard, nút gửi cố định ở cuối trang:

app/settings/page.tsx
<ProForm layout="vertical" onFinish={save} submitter={{ render: (_, dom) => <FooterToolbar>{dom}</FooterToolbar> }}> <ProCard title="Công ty" headerBordered>{/* … */}</ProCard> <ProCard title="Hoá đơn" headerBordered>{/* … */}</ProCard> </ProForm>

Hiện lỗi server

API trả lỗi 4xx với nội dung { error, message, code }. Bọc onFinish bằng submit từ app/hooks: hàm gọi API, báo thành công, tải lại bảng và trả true để đóng form. Khi lỗi, server gửi kèm lỗi từng ô thì hiện ngay dưới ô đó, còn lại hiện message của server, và form không đóng.

app/requests/request-form.tsx
<DrawerForm form={form} title="Đề xuất mới" onFinish={submit((v) => api.requests.create(v), { done: "Đã gửi đề xuất", reload: tableRef, form })}> {/* … */} </DrawerForm>

Cần câu lỗi để hiện ở chỗ khác thì dùng errorText(e) từ manh/shell.

Form nhiều bước

StepsForm chia luồng dài thành nhiều bước. Mỗi StepsForm.StepForm kiểm field của bước đó trước khi sang bước sau. Bước cuối là Result nên ẩn nút ở bước này.

Form nhiều bướcForm nhiều bước: StepsForm (điền → xác nhận → kết quả). Bước cuối ẩn nút, hiện Result.
ví-dụ.manh.run · dữ liệu mẫuMở để sửa ↗
Ví dụ chạy khi bạn cuộn tới
app/onboarding/page.tsx
<StepsForm onFinish={async (v) => { await api.shops.create(v); return true; }} submitter={{ render: (p, dom) => (p.step === 2 ? null : dom) }}> <StepsForm.StepForm name="info" title="Điền thông tin">{/* … */}</StepsForm.StepForm> <StepsForm.StepForm name="confirm" title="Xác nhận">{/* Descriptions + Alert */}</StepsForm.StepForm> <StepsForm.StepForm name="done" title="Hoàn tất"><Result status="success" title="Đã tạo cửa hàng" /></StepsForm.StepForm> </StepsForm>

Cần biết

  • Nếu chỉ có destroyOnHidden, mở lại ModalForm / DrawerForm thì dữ liệu cũ vẫn còn trong field. Với form tạo mới, thêm formRef.current?.resetFields() trong onOpenChange(false), hoặc dùng drawerProps.destroyOnClose / modalProps.destroyOnClose và chịu một cảnh báo khi chạy ở chế độ dev.
  • Không render nguyên đối tượng lỗi. Truyền object làm phần tử con thì React báo lỗi và trang bị sập.
  • Tiền: ProFormMoney (customSymbol="₫", locale="vi-VN", precision: 0); số: ProFormDigit; khoảng ngày: ProFormDateRangePicker.
  • Đặt chữ trên nút bằng từ người dùng hay nói, qua submitter.searchConfig.

Bước tiếp theo

Cập nhật