You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If we could pass hidden values as a prop to the form component, it would make it viable to share most if not all form schemas with trpc-like backends which could make synchronization between front end and backend very easy in cases where the backend is using zod schemas for input.
For example, imagine we had a "comment" form that created a comment for a post. The form only needs to include a text field in the UI, but the backend expects both the text of the comment and the ID of the post:
import{MyCommentSchema}from'schema';// import schema from shared package / file// trpc routerconsttrpcRouter=t.router({postComment: z.procedure.input(MyCommentSchema).mutation(/* do stuff */)})
In our next.js page for example:
import{MyCommentSchema}from'schema';// import schema from shared package / file// front endconstMyPage=()=>{constmutation=api.postComment.useMutation()const{query: {postId}}=useRouter()return(<Formschema={MyCommentSchema}onSubmit={mutation.mutate}hiddenValues={{postId: postId,}}/>)}
No component would be rendered for the postId field (since we don't want users to have to input hidden values), and the hidden value would get passed to the onSubmit callback.
Any additional changes to our shared schema would both add the field to the input of the trpc router, and also render a new field on the frontend. Enables fullstack typesafe form submission with a single source of truth.
The text was updated successfully, but these errors were encountered:
If we could pass hidden values as a prop to the form component, it would make it viable to share most if not all form schemas with trpc-like backends which could make synchronization between front end and backend very easy in cases where the backend is using zod schemas for input.
For example, imagine we had a "comment" form that created a comment for a post. The form only needs to include a text field in the UI, but the backend expects both the text of the comment and the ID of the post:
In our next.js page for example:
No component would be rendered for the
postId
field (since we don't want users to have to input hidden values), and the hidden value would get passed to theonSubmit
callback.Any additional changes to our shared schema would both add the field to the input of the trpc router, and also render a new field on the frontend. Enables fullstack typesafe form submission with a single source of truth.
The text was updated successfully, but these errors were encountered: