Skip to content
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

Add the submitter value with name to formData for use:enhance #7012

Merged
merged 2 commits into from
Sep 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sharp-beers-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Add the submitter value with name to formData for use:enhance
6 changes: 6 additions & 0 deletions packages/kit/src/runtime/app/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export function enhance(form, submit = () => {}) {
);

const data = new FormData(form);

const submitter_name = event.submitter?.getAttribute('name');
if (submitter_name) {
data.append(submitter_name, event.submitter?.getAttribute('value') ?? '');
}

const controller = new AbortController();

let cancelled = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@ export const actions = {
},
slow: async () => {
await new Promise((resolve) => setTimeout(resolve, 500));
},
submitter: async ({ request }) => {
const fields = await request.formData();
return {
result: 'submitter: ' + fields.get('submitter')
};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<input name="username" type="text" />
<button class="form1">Submit</button>
<button class="form1-register" formAction="?/register">Submit</button>
<button class="form1-submitter" formAction="?/submitter" name="submitter" value="foo">Submit</button>
</form>

<span class="count">{count}</span>
Expand Down
15 changes: 15 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,21 @@ test.describe('Actions', () => {
);
});

test('use:enhance button with name', async ({ page, app }) => {
await page.goto('/actions/enhance');

expect(await page.textContent('pre.formdata1')).toBe(JSON.stringify(null));

await Promise.all([
page.waitForRequest((request) => request.url().includes('/actions/enhance')),
page.click('button.form1-submitter')
]);

await expect(page.locator('pre.formdata1')).toHaveText(
JSON.stringify({ result: 'submitter: foo' })
);
});

test('redirect', async ({ page }) => {
await page.goto('/actions/redirect');

Expand Down