-
-
Notifications
You must be signed in to change notification settings - Fork 363
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 Request]: Transform values on submit #418
Comments
Hey @crutchcorn, mind assigning me this for me to work on? 🙏 |
Done! Thanks for taking this on @CheRayLiu. Something worth considering is how we will/should handle TypeScript typings for transformations. |
Unassigned myself for now as I don't see myself working on it any time soon |
No worries @CheRayLiu! Thanks for being willing in the first place :) |
Just an idea for this feature. In my case, every field can have a The benefit of this is, that the typing would be a lot easier, since the Maybe that might be something worth considering implementing here as well. |
Hey @crutchcorn is this issue still up to date? If so, I think I can try to implement a small part of it. |
IMO further discussion should go into whether or not transforms on the base schema should be respected. This is how I personally would like it to work: When we define a zod schema as the validator, the form should allow default values which align with z.input See my code stackblitz: https://stackblitz.com/edit/tanstack-form-zvsfcpvd?file=src%2Findex.tsx |
I think the idea with There is a proposed solution which uses Looking at standard-schema there is My use case: const defaultValues = {
name: "",
related_entry_id: null, // <-- this breaks, it's not a number
// Current workaround: casting as number
// related_entry_id: null as unknown as number,
};
const schema = z.object({
name: z.string().min(3),
related_entry_id: z.number(), // or whatever kind of id you are using, not nullable though
});
const form = useForm({
defaultValues, // this should use InferInput
validators: {
onBlur: schema,
onSubmitAsync: ({ value }) => {
// _ideally_ this would also be InferOutput as by tanstack-form semantics it passed sync validation of the schema already. But that would be the cherry on top.
try {
const response = await saveToDb(value);
return;
} catch (err) {
return err.message;
}
}
},
onSubmit: ({ value }) => {
// This should use InferOutput
toast.success(`You created ${value.name} with related id ${value.related_entry_id}`);
}
}); Unfortunately I fear my TypeScript foo is not good enough to contribute this right now. Do more people have this issue? |
Description
Ideally, I'd like to have a number, displayed as a string (say, a locale with
1,000,000
) and then transform this number back during the field submission.The text was updated successfully, but these errors were encountered: