onMutationError in useFormonMutationError callback in useForm is called after a mutation fails. This can be useful for catching errors during form submission.errorNotificationerrorNotification property in useForm.onFinishhandleOnFinish method, you can wrap it in a try...catch block to handle errors and display notifications using the useNotification hook.validateFields for Step FormsvalidateFields to catch validation errors before proceeding to the next step.dataProvider returns a rejected promise with an errors field. The useForm hook will automatically update the error state with the rejected errors field.rules property of the Form.Item component to define validation rules. Additionally, you can handle server-side validation errors by ensuring your dataProvider returns errors in a specific format. Here are the steps and examples:rules PropertyForm.Item component using the rules property. This allows you to catch and display errors for specific fields.validator Functionvalidator function within the rules property.dataProvider returns a rejected promise with an errors field. The useForm hook will automatically update the error state with the rejected errors field.setFields MethodsetFields method of the FormInstance.handleOnFinishtry...catchuseNotificationvalidateFieldsvalidateFieldserrorserrorserrorserrorsrulesrulesrulesrulesvalidatorvalidatorsetFieldssetFieldsFormInstanceuseForm({
onMutationError: (data, variables, context, isAutoSave) => {
console.log({ data, variables, context, isAutoSave });
},
}); useForm({
action: "create",
resource: "post",
errorNotification: (data, values, resource) => {
return {
message: `Something went wrong when deleting ${data.id}`,
description: "Error",
type: "error",
};
},
}); const handleOnFinish = async (values) => {
try {
// Your custom logic
await onFinish({
fullName: `${values.name} ${values.surname}`,
});
} catch (error) {
// Handle error
notification.error({
message: 'Error',
description: error.message,
});
}
};<Button
onClick={() => {
form.validateFields()
.then(() => gotoStep(current + 1))
.catch((error) => {
// Handle error
console.error(error);
});
}}
>
Next
</Button>npm create refine-app@latest -- --example server-side-form-validation-antdimport { Form, Input } from "antd";
<Form>
<Form.Item
label="Title"
name="title"
rules={[
{ required: true, message: "Title is required" },
{ min: 5, message: "Title must be at least 5 characters" },
]}
>
<Input />
</Form.Item>
...
</Form><Form>
<Form.Item
label="Title"
name="title"
rules={[
{
validator: async (_, value) => {
if (!value || value.length < 5) {
return Promise.reject(new Error("Title must be at least 5 characters"));
}
// Add custom validation logic here
return Promise.resolve();
},
},
]}
>
<Input />
</Form.Item>
...
</Form>import { HttpError } from "@refinedev/core";
const error: HttpError = {
message: "An error occurred while updating the record.",
statusCode: 400,
errors: {
title: ["Title is required"],
content: {
key: "form.error.content",
message: "Content is required.",
},
tags: true,
},
};import { Form } from "antd";
const form = Form.useFormInstance();
const setErrors = (errorData) => {
Object.entries(errorData).forEach(([field, errors]) => {
form.setFields([{ name: field, errors: errors ?? [] }]);
});
};
// Example usage
setErrors({
title: ["Title is required"],
content: ["Content is required"],
});