Skip to Content

submit

The onFinish handler for ProForm, ModalForm and DrawerForm: it calls the API, says it worked, reloads the table, puts server errors on the right fields and resolves true or false.

Usage

app/orders/page.tsx
import { useRef } from "react"; import { ModalForm, ProForm, ProFormText, ProFormDigit, ProTable, type ActionType } from "@ant-design/pro-components"; import { Button } from "antd"; import { useApiClient } from "app/api"; import { submit, tableRequest } from "app/hooks"; export default function OrdersPage() { const api = useApiClient(); const ref = useRef<ActionType>(); const [form] = ProForm.useForm(); const create = ( <ModalForm title="New order" form={form} trigger={<Button type="primary">New order</Button>} onFinish={submit((v) => api.orders.create(v), { done: "Order created", reload: ref, form })}> <ProFormText name="customerName" label="Customer" rules={[{ required: true }]} /> <ProFormDigit name="total" label="Total" min={0} /> </ModalForm> ); return <ProTable rowKey="id" actionRef={ref} request={tableRequest(api.orders.list)} toolBarRender={() => [create]} />; }

Signature

app/hooks.ts
function submit<V>( run: (values: V) => Promise<any>, o?: { done?: string; reload?: { current?: { reload?: () => void } | null }; form?: any }, ): (values: V) => Promise<boolean>;

Parameters

  • run: takes the form values and calls the API, such as (v) => api.orders.update(row.id, v).
  • o.done: the success message, such as “Saved”. Leave it out to show nothing.
  • o.reload: the actionRef of the ProTable to reload after success.
  • o.form: the form instance from ProForm.useForm(), also passed to that form’s form prop. With it, the server’s per-field errors show right under the matching inputs.

Returns

A function to pass straight to onFinish. When run succeeds it shows done, reloads the table and resolves true, so the form closes. On error it resolves false and the form stays open:

  • A 400 or 422 with fields, and o.form given: each error shows under its field.
  • Anything else: the server’s message, or “Có lỗi xảy ra, vui lòng thử lại” when the server sent none.

submit doesn’t throw, and doesn’t show an error twice when the AppShell frame already showed it.

Examples

An edit form in a table row:

app/orders/page.tsx
<ModalForm title="Edit order" initialValues={row} trigger={<a>Edit</a>} onFinish={submit((v) => api.orders.update(row.id, v), { done: "Saved", reload: ref })}> <ProFormText name="customerName" label="Customer" /> </ModalForm>

A handler’s 400 validation error already carries fields with Vietnamese messages, see Error codes. Don’t wrap forms in try { … } catch { message.error(…) }.

Next steps

  • useAction: buttons and forms that are not create or edit.
  • tableRequest: the table reload refreshes.
  • optionsFrom: select fields whose options come from the API.
Last updated