Next.js Discord

Discord Forum

Unhelpful server action error with form submission

Unanswered
Tomistoma posted this in #help-forum
Open in Discord
TomistomaOP
I need to build a form fairly quickly and I don't have much time to reinvent the wheel, as much as I would like to. For that reason I've just gone with react-hook-form, zod and server actions. I don't want to get into whether that's a good combination/suitable as I would prefer to not use it but I hoped it would be an easier solution.
I'm currently getting the classic error that functions cannot be passed directly to client components but it's not pointing out exactly where this is happening and I'm failing to find it with my own eyes. Here's the error:
Error: Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server". Or maybe you meant to call this function rather than return it.
  [[...], function, function, ...]
                    ^^^^^^^^

The array of functions is not a helpful indicator this, I don't think.
I'm creating my client form like so:
"use client";
// ...

const resolver = zodResolver(SCHEMA); // the schema is a shared export from a TS file used on the client and server

export const TestForm = ({ onSubmit }: FormProps) => {
  const [state, action, isPending] = useActionState<Resp<unknown> | null, FormData>(
    onSubmit,
    null,
  );

  const {
    register,
    formState: { errors },
  } = useForm<FormType>({
    mode: 'onBlur',
    resolver,
  });

  return (
    <Form action={action}>
      <Input {...register('name')} hasError={!!errors.name} type="text" />
      <button type="submit" disabled={isPending}>
        Submit
      </button>
    </Form>
  );
};

and I construct this form component in the page.tsx file as shown:
const Page = async () => {
  return <TestForm onSubmit={formActionSubmit} />;
};

... continued in comment

6 Replies

TomistomaOP
....
The formActionSubmit server action is nothing complicated either:
"use server";
// ...

export const formActionSubmit = async (
  prevState: unknown,
  formData: FormData,
): Resp<unknown> => {
  const dataValidation = SCHEMA.safeParse({
    name: formData.get('name'),
  });

  if (!dataValidation.success) {
    return failure('Invalid form data.', { details: Object.values(dataValidation.error) });
  }

  // ....

  return success({});
};

This is obviously simpler than the real form I am using, but the same error is reproducable with this. No matter how many different tutorials I follow I cannot get this error to go away. I can't see where the issue is myself, so is anyone else able to point it out?
try importing directly formActionSubmit inside formActionSubmit instead of passing it thru props
TomistomaOP
no luck, same error
then maybe is the action you are passing to Form
try normal form instead of Form
<form action={action}>
TomistomaOP
unfortunately that hasn’t helped either
i might just have to look into making my own RHF-like hook that works with actions as i can’t find anything online about zod client+server validation on next15