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

Feature - Add "Hidden Values" #83

Open
iway1 opened this issue Mar 12, 2023 · 1 comment
Open

Feature - Add "Hidden Values" #83

iway1 opened this issue Mar 12, 2023 · 1 comment

Comments

@iway1
Copy link
Owner

iway1 commented Mar 12, 2023

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:

export const MyCommentSchema = z.object({
  text: z.string(),
  postId: z.string(),
})
import { MyCommentSchema } from 'schema'; // import schema from shared package / file
// trpc router
const trpcRouter = 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 end

const MyPage = ()=>{
  const mutation = api.postComment.useMutation()
  const {query: {postId}} = useRouter()
  return (
    <Form 
      schema={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.

@tonoli
Copy link

tonoli commented Feb 25, 2024

This could be very nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants