Form: Transform data before submit
Is it possible to transform data before submit with a useForm + antd ?
useForm with Ant Design in Refine. You can achieve this by creating a custom function where you modify the form data as needed. Here is an example:name and , are combined and sent to the API as source (https://refine.dev/docs/ui-integrations/ant-design/hooks/use-form#how-can-i-change-the-form-data-before-submitting-it-to-the-api).useForm, which submits the form data to the API.onFinishonFinishsurnamefullNamehandleOnFinishimport { Create, useForm } from "@refinedev/antd";
import { Form, Input } from "antd";
import React from "react";
export const UserCreate: React.FC = () => {
const { formProps, saveButtonProps, onFinish } = useForm();
const handleOnFinish = (values) => {
onFinish({
fullName: `${values.name} ${values.surname}`,
});
};
return (
<Create saveButtonProps={saveButtonProps}>
<Form {...formProps} onFinish={handleOnFinish} layout="vertical">
<Form.Item label="Name" name="name">
<Input />
</Form.Item>
<Form.Item label="Surname" name="surname">
<Input />
</Form.Item>
</Form>
</Create>
);
}; import { Create, useForm } from "@refinedev/antd";
import { Form, Checkbox } from "antd";
import React from "react";
export const UserCreate: React.FC = () => {
const { formProps, saveButtonProps, onFinish } = useForm();
const handleOnFinish = (values) => {
const transformedValue = values.checkbox ? new Date() : null;
onFinish({
checkbox: transformedValue,
});
};
return (
<Create saveButtonProps={saveButtonProps}>
<Form {...formProps} onFinish={handleOnFinish} layout="vertical">
<Form.Item label="Checkbox" name="checkbox" valuePropName="checked">
<Checkbox />
</Form.Item>
</Form>
</Create>
);
};