Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Note $ACTION_ formData properties #63048

Merged
merged 3 commits into from
Mar 8, 2024
Merged

Docs: Note $ACTION_ formData properties #63048

merged 3 commits into from
Mar 8, 2024

Conversation

karlhorky
Copy link
Contributor

@karlhorky karlhorky commented Mar 8, 2024

What?

Make a note about the new $ACTION_ properties in the formData

Why?

Since next@14.1.3, some new $ACTION_ properties have appeared in the formData in development mode, which cause validation errors when used with a strict validation with a library such as Zod or Yup:

// 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()

{
  '$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, which was a part of this PR:

cc @delbaoliveira

@karlhorky karlhorky requested review from a team as code owners March 8, 2024 14:10
@karlhorky karlhorky requested review from timeyoutakeit and molebox and removed request for a team March 8, 2024 14:10
@ijjk
Copy link
Member

ijjk commented Mar 8, 2024

Allow CI Workflow Run

  • approve CI run for commit: 5fd7a5f

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@karlhorky
Copy link
Contributor Author

karlhorky commented Mar 8, 2024

Workaround

In future, these properties with the $ACTION_ prefix may be excluded from the formData which is available in the Server Action.

In the meantime, instead of Object.fromEntries(formData.entries()), filter out the formData properties that start with $ACTION_:

// TODO: Revert to `Object.fromEntries(formData.entries())` if `$ACTION_`
// properties are omitted from Next.js dev mode Server Actions formData in future
// https://github.com/vercel/next.js/pull/63048#issuecomment-1986138940
Object.fromEntries(
  [...formData.entries()].filter(([key]) => !key.startsWith('$ACTION_')),
)

@samcx
Copy link
Member

samcx commented Mar 8, 2024

@karlhorky Taking a look! :frog-eyes:

Copy link
Member

@samcx samcx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for submitting a :pr:!

:lgtm:

@samcx samcx enabled auto-merge (squash) March 8, 2024 23:03
@samcx samcx merged commit 47b4668 into vercel:canary Mar 8, 2024
28 checks passed
@karlhorky karlhorky deleted the patch-2 branch March 9, 2024 09:17
@karlhorky
Copy link
Contributor Author

Thanks for the review and merge @samcx 🙌

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants