Skip to content

Commit 814ced4

Browse files
authored
templates: allow displaying dynamic error message on forms created via Form Builder plugin (#11275)
Close #11274 ### Why this PR? I've created a custom phone number input block for my form builder, including validation. However, the component on the frontend only displays the generic message "This field is required," even when formState.errors contains specific error messages. This is not the expected behavior. I need the component to display the error messages from formState.errors. ### Description This pull request includes changes to improve error handling in various form components by passing the `name` prop to the `Error` component and updating the `Error` component to display specific error messages. #### Error handling improvements: * [`templates/website/src/blocks/Form/Error/index.tsx`](diffhunk://#diff-a97a4b2b87ff1a02431d11ab00f4e0ead5d11819f45dac120b9502ace520196fR1-R14): Updated the `Error` component to accept a `name` prop and use `useFormContext` to display specific error messages. #### Form component updates: * [`templates/website/src/blocks/Form/Checkbox/index.tsx`](diffhunk://#diff-4f0ad9596965f1e3b2f6356943d1d34009a742502bc8ab8d438ce98593fdef4aL42-R42): Modified to pass the `name` prop to the `Error` component. * [`templates/website/src/blocks/Form/Country/index.tsx`](diffhunk://#diff-3abd97c2bfe7ce2a1809e6eaac68e6c02078514308f964b1792f7a1af2df92a7L62-R62): Modified to pass the `name` prop to the `Error` component. * [`templates/website/src/blocks/Form/Email/index.tsx`](diffhunk://#diff-f1be3cf1e7c1fa9b543ed8f56a3655e601fdb399d31ede1d099a37004a1861bfL35-R35): Modified to pass the `name` prop to the `Error` component. * [`templates/website/src/blocks/Form/Number/index.tsx`](diffhunk://#diff-72e5bd63eda769bce077e87bc614cb338211600580ad38ba86a7f066a35212a5L33-R33): Modified to pass the `name` prop to the `Error` component. * [`templates/website/src/blocks/Form/Select/index.tsx`](diffhunk://#diff-69d52ba3bb01fc0ce4428f5b76ab48a86c448dceaf36390edbcf345f0b15c34eL60-R60): Modified to pass the `name` prop to the `Error` component. * [`templates/website/src/blocks/Form/State/index.tsx`](diffhunk://#diff-c0eb5a8c64b6384a44e19556556921bff4c89ed3a8d5a1d2e46ce493178587caL61-R61): Modified to pass the `name` prop to the `Error` component. * [`templates/website/src/blocks/Form/Text/index.tsx`](diffhunk://#diff-9d32d5b3132729534809280d97d8a0952e96270f434b5d57a32a2d4981a36384L29-R29): Modified to pass the `name` prop to the `Error` component. * [`templates/website/src/blocks/Form/Textarea/index.tsx`](diffhunk://#diff-d25c7cb831ee04c195983c1a88718bdcec8f1dc34c3e5237875678eb8194994dL37-R37): Modified to pass the `name` prop to the `Error` component.
1 parent 3de1636 commit 814ced4

File tree

9 files changed

+21
-11
lines changed

9 files changed

+21
-11
lines changed

templates/website/src/blocks/Form/Checkbox/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const Checkbox: React.FC<
3939
{label}
4040
</Label>
4141
</div>
42-
{errors[name] && <Error />}
42+
{errors[name] && <Error name={name} />}
4343
</Width>
4444
)
4545
}

templates/website/src/blocks/Form/Country/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const Country: React.FC<
5959
}}
6060
rules={{ required }}
6161
/>
62-
{errors[name] && <Error />}
62+
{errors[name] && <Error name={name} />}
6363
</Width>
6464
)
6565
}

templates/website/src/blocks/Form/Email/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const Email: React.FC<
3232
{...register(name, { pattern: /^\S[^\s@]*@\S+$/, required })}
3333
/>
3434

35-
{errors[name] && <Error />}
35+
{errors[name] && <Error name={name} />}
3636
</Width>
3737
)
3838
}
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
'use client'
2+
13
import * as React from 'react'
4+
import { useFormContext } from 'react-hook-form'
25

3-
export const Error: React.FC = () => {
4-
return <div className="mt-2 text-red-500 text-sm">This field is required</div>
5-
}
6+
export const Error = ({ name }: { name: string }) => {
7+
const {
8+
formState: { errors },
9+
} = useFormContext()
10+
return (
11+
<div className="mt-2 text-red-500 text-sm">
12+
{(errors[name]?.message as string) || 'This field is required'}
13+
</div>
14+
)
15+
}

templates/website/src/blocks/Form/Number/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const Number: React.FC<
3030
type="number"
3131
{...register(name, { required })}
3232
/>
33-
{errors[name] && <Error />}
33+
{errors[name] && <Error name={name} />}
3434
</Width>
3535
)
3636
}

templates/website/src/blocks/Form/Select/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const Select: React.FC<
5757
}}
5858
rules={{ required }}
5959
/>
60-
{errors[name] && <Error />}
60+
{errors[name] && <Error name={name} />}
6161
</Width>
6262
)
6363
}

templates/website/src/blocks/Form/State/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const State: React.FC<
5858
}}
5959
rules={{ required }}
6060
/>
61-
{errors[name] && <Error />}
61+
{errors[name] && <Error name={name} />}
6262
</Width>
6363
)
6464
}

templates/website/src/blocks/Form/Text/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const Text: React.FC<
2626
)}
2727
</Label>
2828
<Input defaultValue={defaultValue} id={name} type="text" {...register(name, { required })} />
29-
{errors[name] && <Error />}
29+
{errors[name] && <Error name={name} />}
3030
</Width>
3131
)
3232
}

templates/website/src/blocks/Form/Textarea/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const Textarea: React.FC<
3434
{...register(name, { required: required })}
3535
/>
3636

37-
{errors[name] && <Error />}
37+
{errors[name] && <Error name={name} />}
3838
</Width>
3939
)
4040
}

0 commit comments

Comments
 (0)