-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Form actions #6469
Form actions #6469
Conversation
🦋 Changeset detectedLatest commit: f2cdab2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
You can provide your own callback to the enhance action and set a variable to pending during fetching. When the return callback function is called you know the fetch is done. |
in docs in file:
Should we write?
|
Ooh, that likely explains why my experiments did not work as expected (and also #6611). I was wondering whether forms is typed automatically. Having seen the incredible typing magic by @dummdidumm I assume it is, and that's probably why we return validation errors instead of throwing them? |
This should be mentioned in the migration guide. |
Yes that was it, and that fixed it! Thanks
Sent via Superhuman iOS ( ***@***.*** )
…On Thu, Sep 8 2022 at 7:06 AM, vipero07 < ***@***.*** > wrote:
>
>
> @rowantrollope ( https://github.com/rowantrollope ) Check your svelte.config.js
> and make sure you are no longer adding the method overrides for PATCH and DELETE
> . The snippet below is possibly what you have if you have an older project
> (as I did!).
>
> // Override http methods
> methodOverride: {
> allowed: ['PATCH',
> 'DELETE']
> }
>
This should be mentioned in the migration guide.
—
Reply to this email directly, view it on GitHub (
#6469 (comment) ) , or unsubscribe
(
https://github.com/notifications/unsubscribe-auth/ACXKL5KPCKIZXKCNE5XSTWLV5HXE7ANCNFSM6AAAAAAQBN573Y
).
You are receiving this because you were mentioned. Message ID: <sveltejs/kit/pull/6469/c1240765956
@ github. com>
|
1. Remove methodOverride from svelte.conf.js Ref: sveltejs/kit#6469 2. Rename hooks.js to hooks.server.js Ref: sveltejs/kit#6586 3. Change post not found status code from 400 to 404 4. Remove useless import { page } in +layout.svelte
So what about array-data inside my form? |
Ok, got it. FormData.getAll(). |
Does |
Step 2 should import fail, not invalid |
May want to address JSON POST endpoints for the conversion guide? In my case I needed to use |
See #7494 for that, I added a note to the migration description |
How to migrate
The
POST/PUT/PATCH/DELETE
methods on+page.server.js
are replaced by anactions
object.If you used those methods for form submissions
Step 0 (before updating)
Remove method overrides from your
svelte.config.js
(they have been removed, see the other steps on how to migrate) - there have been reports that running npm install after bumping the version with this config still present fails.Step 1
If you have more than one form on that page, think about names that best describe it and add these names as keys to the new action object inside
+page.server.js
. Example:If you only have one form, you don't need to give it a name, use
default
:Step 2
Move your methods over to the respective names of the actions object. Adjust the return value accordingly:
return { location: 'X' }
withthrow redirect(303, 'X')
return { status: 400, errors: errors }
withreturn fail(400, errors)
(return invalid(400, errors)
if you are not migrating to the latest version of SvelteKit directly)The success response and invalid response both become available on
export let forms
(see step 4 for more info).Step 3
Update your
<form>
elements in+page.svelte
(or components used by it), if you were previously usingPUT
/PATCH
/DELETE
and are now using named actions:The
/editTodo
is a normal query parameter. SvelteKit interprets a query parameter with a leading/
as identifying a named action.?/editTodo
will call theeditTodo
action inexport const actions = { editTodo: ..., ... }
Step 4
Replace
export let errors
withexport let form
in+page.svelte
:This will contain the return value of the action that just ran (as long as it didn't error or redirect), allowing you to show a 'success' message or (in the case of validation errors) fill in previously submitted values and indicate which fields were invalid.
Example:
This from the server..
... will become available on the component:
If you used those methods for something else
Either adjust your urls and only make POST requests with
FormData
to it, ot move these methods over to a+server.js
file. We will very soon make it possible to colocate a+server.js
file next to a+page.svelte
file. The return value needs to be aResponse
object; you can use thejson
helper method to make creation of that easier.Manually making the fetch requests
use:enhance
should do all of this for you. If you're on the latest version of SvelteKit and want to do it, see #7494 on how.PR description
Implements #5875
Closes #3533
TODO
export const actions
export let errors
in favor ofexport let form
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. All changesets should bepatch
until SvelteKit 1.0