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

feat: Warn users when submitting forms with files but no enctype="multipart/form-data" #9888

Merged
merged 15 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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/tasty-llamas-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
elliott-with-the-longest-name-on-github marked this conversation as resolved.
Show resolved Hide resolved
---

[feat] Warn users when enhancing forms with files but no `enctype="multipart/form-data"`
11 changes: 11 additions & 0 deletions packages/kit/src/runtime/app/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ export function enhance(form, submit = () => {}) {

const data = new FormData(form);

if (DEV && form.enctype !== 'multipart/form-data') {
elliott-with-the-longest-name-on-github marked this conversation as resolved.
Show resolved Hide resolved
for (const value of data.values()) {
if (value instanceof File) {
// TODO 2.0: Upgrade to `throw Error`
console.warn(
'Your form contains <input type="file"> fields, but is missing the `enctype="multipart/form-data"` attribute. This will lead to inconsistent behavior between enhanced and native forms. For more details, see https://github.com/sveltejs/kit/issues/9819. This will be upgraded to an error in v2.0.'
);
}
}
}

const submitter_name = event.submitter?.getAttribute('name');
if (submitter_name) {
data.append(submitter_name, event.submitter?.getAttribute('value') ?? '');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const actions = {
default: async ({ request }) => {
const data = await request.formData();
console.log(data.get('file'));
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
import { enhance } from '$app/forms';
</script>

<form method="POST" use:enhance>
<input name="file" type="file" />
<button type="submit">upload</button>
</form>
14 changes: 14 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,20 @@ test.describe('Matchers', () => {
});

test.describe('Actions', () => {
test('Submitting a form with a file input but no enctype="multipart/form-data" logs a warning', async ({
page,
javaScriptEnabled
}) => {
if (!javaScriptEnabled || !process.env.DEV) return;
await page.goto('/actions/file-without-enctype');
const logPromise = page.waitForEvent('console');
await page.click('button');
const log = await logPromise;
expect(log.text()).toBe(
'Your form contains <input type="file"> fields, but is missing the `enctype="multipart/form-data"` attribute. This will lead to inconsistent behavior between enhanced and native forms. For more details, see https://github.com/sveltejs/kit/issues/9819. This will be upgraded to an error in v2.0.'
);
});

test('Error props are returned', async ({ page, javaScriptEnabled }) => {
await page.goto('/actions/form-errors');
await page.click('button');
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/dev-only/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "test-basics",
"name": "test-dev-only",
"private": true,
"version": "0.0.2-next.0",
"scripts": {
Expand Down