Creating a form that handles POSTs that works without wasm enabled #3609
Answered
by
benwis
steadygaze
asked this question in
Q&A
Replies: 2 comments 4 replies
-
I would suggest redirecting in the server function after receiving the POST request, and include whatever data you want to show in the url/cookies/etc.
…On Fri, Feb 14, 2025, at 4:39 PM, Steady Gaze wrote:
*tldr* I want to create a POST version of a simple form like The <Form> Component <https://book.leptos.dev/router/20_form.html> chapter of the Leptos book that still works when wasm is disabled.
Hello, I'm in the process of creating some forms to handle email login codes. The general flow is
1. User enters their email into the form and submits it.
2. We email the user a login code and ask the user for it.
3. The user enters the login code and submits it.
4. We log them in/give them a session cookie or redirect them to register, etc.
(POST is better than GET for this use case because I don't want to show the form inputs in the address bar, the user's browsing history, etc., and submitting the same inputs again isn't necessarily repeatable as the login code is temporary/ephemeral.)
I created a form (a couple of `ActionForm` components guarded by `Show`) that entirely relied on server actions to handle POSTing and showing the second stage of the form, but then I realized that this doesn't "gracefully degrade"/still work if wasm is disabled. When a user submits an `ActionForm` with wasm disabled, it does POST to the endpoint but also seems to just refresh the existing page. To work around this, I realized I could make the different stages live at separate URLs and have the previous stage POST to the next stage. In the case that wasm is disabled but we still use SSR, this would render on the server and still work perfectly. The problem is that a `#[component]` can only handle a GET. If I try posting to an endpoint that I set up the Leptos router for, it simply 404s. I looked at `leptos_router` and the `leptos_actix` integration, and also didn't see anything that looked relevant for enabling the POST verb for a component.
In the Leptos book, there is a good example:
async fn fetch_results() {
// some async function to fetch our search results
}
#[component]
pub fn FormExample() -> impl IntoView {
// reactive access to URL query strings
let query = use_query_map();
// search stored as ?q=
let search = move || query.read().get("q").unwrap_or_default();
// a resource driven by the search string
let search_results = Resource::new(search, |_| fetch_results());
view! {
<Form method="GET" action="">
<input type="search" name="q" value=search/>
<input type="submit"/>
</Form>
<Transition fallback=move || ()>
/* render search results */
{todo!()}
</Transition>
}
}
As a user, I want to be able to just change `method="GET"` to `method="POST"` and `use_query_map` to `use_params_map` and have it work pretty much the same. Is this something the framework doesn't support at the moment, or am I wanting to do it the wrong way?
—
Reply to this email directly, view it on GitHub <#3609>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ABVBTCLJW7AYBIRR4QYTJWT2P2EDPAVCNFSM6AAAAABXFWY7NSVHI2DSMVQWIX3LMV43ERDJONRXK43TNFXW4OZXHE3DQMZYGA>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
steadygaze
-
We do actually support POST requests to components, you’d have to exclude it from our automatic routing for Axum/Actix and then write your own route
I would suggest looking at the todo_app examples, they use this pattern. With wasm disabled it returns a 302 redirect to the next page
…On Fri, Feb 14, 2025, at 5:54 PM, Steady Gaze wrote:
It's up to you, but I would consider making it so that components can directly handle POST requests (and potentially other verbs, if you can think of a use for them). I kind of assume it will be too difficult for me to do and touch too many Leptos internals, but if not, I would be willing to contribute something like this if I have the time.
—
Reply to this email directly, view it on GitHub <#3609 (reply in thread)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ABVBTCPUYNG4DTBMCNGF4DD2P2M4FAVCNFSM6AAAAABXFWY7NSVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTEMRQGY4DONQ>.
You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
tldr I want to create a POST version of a simple form like The <Form> Component chapter of the Leptos book that still works when wasm is disabled.
Hello, I'm in the process of creating some forms to handle email login codes. The general flow is
(POST is better than GET for this use case because I don't want to show the form inputs in the address bar, the user's browsing history, etc., and submitting the same inputs again isn't necessarily repeatable as the login code is temporary/ephemeral.)
I created a form (a couple of
ActionForm
components guarded byShow
) that entirely relied on server actions to handle POSTing and showing the second stage of the form, but then I realized that this doesn't "gracefully degrade"/still work if wasm is disabled. When a user submits anActionForm
with wasm disabled, it does POST to the endpoint but also seems to just refresh the existing page. To work around this, I realized I could make the different stages live at separate URLs and have the previous stage POST to the next stage. In the case that wasm is disabled but we still use SSR, this would render on the server and still work perfectly. The problem is that a#[component]
can only handle a GET. If I try posting to an endpoint that I set up the Leptos router for, it simply 404s. I looked atleptos_router
and theleptos_actix
integration, and also didn't see anything that looked relevant for enabling the POST verb for a component.In the Leptos book, there is a good example:
As a user, I want to be able to just change
method="GET"
tomethod="POST"
anduse_query_map
touse_params_map
and have it work pretty much the same. Is this something the framework doesn't support at the moment, or am I wanting to do it the wrong way?Beta Was this translation helpful? Give feedback.
All reactions