Skip to content

Commit 47b4668

Browse files
karlhorkysamcx
andauthored
Docs: Note $ACTION_ formData properties (#63048)
<!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change --> ### What? Make a note about the new `$ACTION_` properties in the `formData` ### Why? Since `[email protected]`, some new `$ACTION_` properties have appeared in the`formData`, which cause validation errors when used with a strict validation with a library such as Zod or Yup: ```tsx // Form Client Component export default function Form() { const [state, formAction] = useFormState(action, { type: 'initial', }); return ( <form action={action}> {/* ... */} </form> ); } // Server Action export async function action( prevState: ServerActionReturnValue, formData: FormData, ): Promise<ServerActionReturnValue> { const validationResult = await schema.validate( Object.fromEntries(formData.entries()), ); ``` The errors from Yup: ``` Errors: - this field has unspecified keys: $ACTION_REF_1, $ACTION_1:0, $ACTION_1:1, $ACTION_KEY ``` The data in the `formData.entries()` ```js { '$ACTION_REF_1': '', '$ACTION_1:0': '{"id":"59f475b...","bound":"$@1"}', '$ACTION_1:1': '[{"type":"initial"}]', '$ACTION_KEY': 'k187677481', // Real form data starts here: year: '2024', ``` ### How? Document additional properties with keys starting with the prefix `$ACTION_` ### Alternatives Considered Maybe these `$ACTION_` properties are not intended to be exposed to users, and should be stripped before the `formData` reaches the Server Action. ### Related The documentation was originally introduced in commit [`687239c`](687239c), which was a part of this PR: - #59080 cc @delbaoliveira --------- Co-authored-by: Sam Ko <[email protected]>
1 parent a767aa8 commit 47b4668

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

docs/02-app/01-building-your-application/02-data-fetching/02-server-actions-and-mutations.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export default function Page() {
171171
> **Good to know:**
172172
>
173173
> - Example: [Form with Loading & Error States](https://github.com/vercel/next.js/tree/canary/examples/next-forms)
174-
> - When working with forms that have many fields, you may want to consider using the [`entries()`](https://developer.mozilla.org/en-US/docs/Web/API/FormData/entries) method with JavaScript's [`Object.fromEntries()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries). For example: `const rawFormData = Object.fromEntries(formData.entries())`
174+
> - When working with forms that have many fields, you may want to consider using the [`entries()`](https://developer.mozilla.org/en-US/docs/Web/API/FormData/entries) method with JavaScript's [`Object.fromEntries()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries). For example: `const rawFormData = Object.fromEntries(formData.entries())`. One thing to note is that the `formData` will include additional `$ACTION_`properties.
175175
> - See [React `<form>` documentation](https://react.dev/reference/react-dom/components/form#handle-form-submission-with-a-server-action) to learn more.
176176
177177
#### Passing Additional Arguments

0 commit comments

Comments
 (0)