react-hook-form: Ignoring FormData in server action and using .bind
Unanswered
Silver posted this in #help-forum
SilverOP
Hi, I'm building a form with shadcn, server actions, and useFieldArray. I have found that using .bind with my type is consistently working more 'easily' than using the formData. Essentially when I'm using formData to parse back into some schema to do zod validation ive found it weird to parse arrays that are nested (maybe even arrays inside arrays) because I have to do some string interpolation and get vscode warnings for type conflicts. is it bad practice to ignore formData and just use bind?
16 Replies
@Silver Hi, I'm building a form with shadcn, server actions, and useFieldArray. I have found that using .bind with my type is consistently working more 'easily' than using the formData. Essentially when I'm using formData to parse back into some schema to do zod validation ive found it weird to parse arrays that are nested (maybe even arrays inside arrays) because I have to do some string interpolation and get vscode warnings for type conflicts. is it bad practice to ignore formData and just use bind?
FormData for user submitted data, .bind for application states you want to send along with the form.
[both need server-side validation, by the way.](https://nextjs.org/blog/security-nextjs-server-components-actions#write) so even your .bind will not help. use something like zod + zod-form-data to (1) validate the data and (2) get fully typed values to use.
[both need server-side validation, by the way.](https://nextjs.org/blog/security-nextjs-server-components-actions#write) so even your .bind will not help. use something like zod + zod-form-data to (1) validate the data and (2) get fully typed values to use.
@joulev FormData for user submitted data, .bind for application states you want to send along with the form.
[both need server-side validation, by the way.](<https://nextjs.org/blog/security-nextjs-server-components-actions#write>) so even your .bind will *not* help. use something like zod + zod-form-data to (1) validate the data and (2) get fully typed values to use.
SilverOP
Hi thank you for the information. let me provide more context it seems im facing a bug rather than a knowledge barrier
2 questions/concerns:
1. Unless I add this to my server action some values are not getting passed into form data, this fixes it. I'm assuming this means theres a bug with a child componnent. but im a little unsure how to debug
2. What is the reccomended approach for parsing nested zod schemas with form data? (the data is getting validated on the client side with zod resolver but not passed through im assuming bc of the child components for technologies used and time are not sending value properly with formData but work when I bind form.GetValues()
example schema
2 questions/concerns:
1. Unless I add this to my server action some values are not getting passed into form data, this fixes it. I'm assuming this means theres a bug with a child componnent. but im a little unsure how to debug
const updateBinded = updateProjects.bind(null, form.getValues()); 2. What is the reccomended approach for parsing nested zod schemas with form data? (the data is getting validated on the client side with zod resolver but not passed through im assuming bc of the child components for technologies used and time are not sending value properly with formData but work when I bind form.GetValues()
example schema
export const DateSchema = z.object({
month: z.number().min(1).max(12),
year: z.number().min(1900).max(new Date().getFullYear()),
day: z.number().min(1).max(31).default(1).nullable().optional(),
});
export const ProjectSchema = z.object({
name: z.string(),
description: z.string(),
technologies_used: z.array(z.string()).optional(),
start_date: DateSchema,
end_date: DateSchema.optional().nullable(),
});
export type Project = z.infer<typeof ProjectSchema>;
export const ProjectsSchema = z.array(ProjectSchema);
export type Projects = z.infer<typeof ProjectsSchema>;formData FormData {
[Symbol(state)]: [
{ name: 'projects.0.name', value: 'Seller Watch' },
{
name: 'projects.0.description',
value: '• Built SellerWatch.io, a subscription-based platform powered with web-scraped data and OpenAI API to identify an estimated 200 daily profitable product leads, tailored to 50+ Amazon sellers’ unique needs\r\n
},
{ name: 'projects.1.name', value: 'Current 2' },
{
name: 'projects.1.description',
value: '• Developed a user-friendly sign-up page using HTML/CSS with custom controls to prevent invalid phon'
}
]
}SilverOP
It also seems that using onsubmit instead of action has seemed to work for some people but for me won't trigger my form. let me provide my files for reference
Any help in this matter is greatly appreciated, thank you
i dont use react hook form so i cant help here, sorry
SilverOP
Is there another recommended approach to achieve what I’m looking for?
@joulev
SilverOP
@Clown im noticing that the form data is inside this 'value' but not inside the keys as u would expect it to be
whats the reccomended way to parse this?
SilverOP
this is what it took.
SilverOP
SilverOP
this honestly feels like each form i make has a different 'method'
there has to be an official way to do this