-
-
Notifications
You must be signed in to change notification settings - Fork 438
[Feature Request]: Transform values on submit #418
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
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? |
@leomelzer More people definitely have this issue. This is exactly the type of behaviour that I am looking for. |
I have discovered an alternative approach, but I am uncertain whether it is an effective strategy. 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
});
type Inputs = z.infer<typeof schema>;
const form = useAppForm({
validators: { onChange: schema },
onSubmit: async ({ value }: { value: Inputs }) => {
console.log(value.related_entry_id) // number
}
}); If you assign a type to a value, you do not need to define default values. |
are there any solutions by any chance? I would go as far as adding the ability to add additional values from validators functions to the onSubmit of useForm. |
This is why I went with |
Why is it clunky? |
I think for complex types it is quite nice and the functional style composition is also nice. But for simple types, e.g. a string with some length, writing type codecs is a bit more complex than something like zod where you can just chain a type together. Personally I am willing to deal with the verbosity of io-ts because I find the encoding/decoding aspect to be a killer feature |
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: