Re-usable forms #13147
Replies: 1 comment
|
I’d strongly lean toward one Your target data shape is already a good fit for RHF: type FormValues = {
register: RegistrationFormValues
project: ProjectFormValues
}Then make the reusable project piece render fields only, and let the parent own the form instance: function ProjectFields({ prefix }: { prefix: 'project' }) {
const { register } = useFormContext<FormValues>()
return (
<input {...register(`${prefix}.title`)} />
)
}const methods = useForm<FormValues>()
<FormProvider {...methods}>
<form onSubmit={methods.handleSubmit(onSubmit)}>
<input {...methods.register('register.username')} />
<ProjectFields prefix="project" />
</form>
</FormProvider>That way you keep one submission, one validation flow, and a reusable field group instead of duplicating the whole form. So I’d think in terms of reusable field sections, not reusable |
|
The clean solution is the The approachDefine your Step 1: ProjectForm uses
|
Uh oh!
There was an error while loading. Please reload this page.
My use-case is as following;
I have a rather complex "Create a Project" form. Some fields are dynamically generated from API queries, the validation schema is also dynamically generated.
This form exists in an authenticated route (
/projects/create) but should also exist in the registration form... From the landing page you can click "Create a project" and in the case of not having an account, you can create a project and register at the same time.. Then all the data is being submitted at once.I don't wanna copy-paste the whole create a project form into the registration page...
One way I thought of is to have two different
useForms in the parent component; one for the project creation and once again for the registration. This is kinda "messy" and feels like a huge workaround but could work I guess?Ideally what I was thinking was something like:
But this would completely destroy type-safety... Are there any good ideas on how to handle that?
All reactions