feat(web): highlight the offending field on a set_fields 422 (#28)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-05 15:39:49 +02:00
parent d6dc1c9b57
commit 0c9db7bcdb
7 changed files with 73 additions and 12 deletions
+13 -1
View File
@@ -1,3 +1,4 @@
import { useEffect } from "react";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
@@ -47,12 +48,14 @@ export function ObjectForm({
onSubmit,
onCancel,
formError,
fieldErrorKey,
}: {
mode: "create" | "edit";
defaults?: { core: ObjectCore; fields: Record<string, unknown> };
onSubmit: (values: ObjectFormValues) => void;
onCancel: () => void;
formError?: string | null;
fieldErrorKey?: string | null;
}) {
const { t } = useTranslation();
@@ -68,6 +71,15 @@ export function ObjectForm({
const { register, handleSubmit, formState: { errors } } = form;
useEffect(() => {
if (fieldErrorKey) {
form.setError(`fields.${fieldErrorKey}` as never, {
type: "server",
message: t("form.fieldRejected", { field: fieldErrorKey }),
});
}
}, [fieldErrorKey, form, t]);
const submit = handleSubmit((data) => {
const fields = pruneFields(data.fields);
@@ -149,7 +161,7 @@ export function ObjectForm({
{errors.fields?.[def.key] && (
<p role="alert" className="text-xs text-red-600">
{t("form.required")}
{errors.fields[def.key]?.message ?? t("form.required")}
</p>
)}
</div>