T
TanStack5mo ago
wise-white

Getting redirected on submitting a form with TanStack start server functions.

Is this a problem on the Start side or am I doing something wrong? I was following the SSR guide on the form docs.
No description
10 Replies
wise-white
wise-whiteOP5mo ago
This is how I use the server function
<form
action={handleLogin.url}
method="POST"
encType="multipart/form-data"
>
<form
action={handleLogin.url}
method="POST"
encType="multipart/form-data"
>
Correction. This happens because of this part in the example:
.validator((data: unknown) => {
if (!(data instanceof FormData)) {
throw new Error("Invalid form data");
}
return data;
})
.handler(async (ctx) => {
try {
const validatedData = await serverValidate(ctx.data);
console.log("validatedData", validatedData);
// TODO: Persist the form data to the database
} catch (e) {
if (e instanceof ServerValidateError) {
// If we use e.response there is a typescript error and if we don't the user is redirected to the above page
// return e.response;
return e.formState;
}

// Some other error occurred when parsing the form
console.error(e);
setResponseStatus(500);
return "There was an internal error";
}

return "Form has validated successfully";
});
.validator((data: unknown) => {
if (!(data instanceof FormData)) {
throw new Error("Invalid form data");
}
return data;
})
.handler(async (ctx) => {
try {
const validatedData = await serverValidate(ctx.data);
console.log("validatedData", validatedData);
// TODO: Persist the form data to the database
} catch (e) {
if (e instanceof ServerValidateError) {
// If we use e.response there is a typescript error and if we don't the user is redirected to the above page
// return e.response;
return e.formState;
}

// Some other error occurred when parsing the form
console.error(e);
setResponseStatus(500);
return "There was an internal error";
}

return "Form has validated successfully";
});
If I return e.response instead of e.formState as the example suggests we get a cryptic typescript error Type '(name: string, value: string) => void' is not assignable to type '"Function is not serializable"'.
extended-salmon
extended-salmon5mo ago
I have the same
wise-white
wise-white5mo ago
Same thing here I get redirected to the server functions URL when submitting the form even after following the example using form + start
extended-salmon
extended-salmon5mo ago
I really don't understand the doc example.à
deep-jade
deep-jade5mo ago
Same, following the docs leads me to an error
wise-white
wise-whiteOP5mo ago
We are supposed to return the e.response. Since Start is in Beta the APIs keep changing and the Forms docs haven't kept up. And to fix the typescript error I think I type-casted something to unknown. But to be honest I gave up on this pattern, I'm just submitting with javascript. Once tanstack starts has integration with react server actions like remix and next it will become easier I reckon. There definitely needs to be a proper integration between Start and Form since the logic for getting back the form state from the server with another server function seems very hacky. If next can do it with useActionState then Start can too at some point.
extended-salmon
extended-salmon5mo ago
Yes indeed its maybe too early. Lets do client side then!
wise-white
wise-whiteOP5mo ago
Its funny the Start team are discussing how to handle this as we speak
extended-salmon
extended-salmon5mo ago
where do you see that ? @Mofo
wise-white
wise-whiteOP5mo ago
They are discussing how to make a thing similar to "server actions" from next/remix Its in the #start channel its more of a future goal tho

Did you find this page helpful?