form action on a different page with use:enhance
#10842
-
Hi, I'm using a form with an action on another page. <form
action="/somewhere/else"
method="post"
>
<button type="submit">
Submit
</button>
</form> It works fine without enhancements, I'm getting redirected to But when I add I tried using https://kit.svelte.dev/docs/form-actions#progressive-enhancement-use-enhance
<form
action="/somewhere/else"
method="post"
use:enhance={({ formElement, formData, action, cancel }) => {
return async ({ result }) => {
await applyAction(result); // ???
};
}}
>
<button type="submit">
Submit
</button>
</form> I'm only getting {
"type": "success",
"status": 200,
"data": {}
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 11 replies
-
Native form submission always navigates to the route specified in the form You could choose to emulate the navigation to the <form
action="/somewhere/else"
method="post"
use:enhance={({ formElement, formData, action, cancel }) => {
return async ({ result }) => {
// emulate native browser form redirect
if (result.type === "success") {
await goto(action);
}
await applyAction(result);
};
}}
>
<button type="submit">
Submit
</button>
</form> |
Beta Was this translation helpful? Give feedback.
-
What wasn't clear to me from the documentation nor the posts I could find online is what the returned response should be from the formaction. I got this form:
To redirect by returning the following in my
Wanted to share in case anybody runs into the same issue and needs a hint. |
Beta Was this translation helpful? Give feedback.
-
I have found another solution. The use case is slightly different but similar. I have a single-button form in +layout.svelte, with page actions in a separate +page.server.js. The buttons can be pressed from any child route. Without JavaScript, the actions should redirect to where the action originated. With JavaScript enabled, the default use:enhance behavior makes a redirect unnecessary (or would require custom logic, as mentioned above) as it is already handled. // /any/path/anywhere/+page.server.js
export const actions = {
some_action: async ({ request }) => {
// do stuff
// error path
const data = await request.formData();
const redirect_path = data.get('redirect-path');
if (redirect_path) {
// redirect THROWS so no further handlers are needed
redirect(303, String(redirect_path));
}
// return path on succes
},
}; and (anywhere basically): <script >
import { browser } from '$app/environment';
import { enhance } from '$app/forms';
import { page } from '$app/state';
</script>
<form method="POST" action="/any/path/anywhere?/some_action" use:enhance>
{#if !browser}
<input type="hidden" name="redirect-path" value={page.url.pathname} />
{/if}
<button type="submit">Do something</button>
</form> The idea is simple: |
Beta Was this translation helpful? Give feedback.
Native form submission always navigates to the route specified in the form
action
attribute. However,use:enhance
only navigates to another page if you explicitly throw a redirect in your action in+page.server.js
.You could choose to emulate the navigation to the
action
route after a successful response with agoto
call https://kit.svelte.dev/docs/modules#$app-navigation-goto