Skip to content

Proposed improvement to handling of file input #1

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

Open
wants to merge 3 commits into
base: daniel/458-allow-set-formenctype
Choose a base branch
from

Conversation

djyotta
Copy link
Owner

@djyotta djyotta commented Jun 27, 2024

Currently, when a file input is present, the default validation (submit) button has formenctype="multipart/form-data" set on it.

This is to ensure that file uploads work without requiring the developer to set he enctype on the form element.

The drawback with doing it this way, is the following won't work as expected:

Multiple submit buttons

SELECT 'form' AS component;
SELECT 'file' AS type, 'data' AS name, '' AS label;
-- the default submit button will work, but this custom submit button won't.
SELECT 'submit' AS type, 'Alternative Submit' AS value, '/other-backend.sql' AS formaction;

External submit buttons

SELECT 'form' AS component, '' AS validate, 'upload' AS id;
SELECT 'file' AS type, 'data' AS name, '' AS label;
-- external buttons don't work
SELECT 'button' AS component, 'upload' AS form;

Overrides form enctype if set

SELECT 'form' AS component
, 'upload' AS id
-- oops! default validate button overrides the form enctype
, 'application/x-www-form-urlencoded' AS enctype;
SELECT 'file' AS type, 'data' AS name, '' AS label;
-- can work around with a custom button
SELECT 'submit' AS type, 'Submit as urlencoded' AS value, 'application/x-www-form-urlencoded'  AS formenctype;
-- or an external button
SELECT 'button' AS component, 'upload' AS form;

So my proposal is to set the enctype of the form to multipart/form-data by default.

This means that form will submit successfully regardless of default validate (submit) button or custom submit buttons or even external submit buttons.

One obvious issue with multipart/form-data as default it that even simple text values in forms will be posted as multipart form data, which will be a significant increase to the payload if there are many form fields with short text values.

To work around this, I propose adding formenctype="application/x-www-form-urlencoded" to the default submit button only if the form enctype is not set.
This would preserve the existing behaviour for users who don't modify form encoding whilst fixing the fact that any custom or external submit buttons currently do not work without setting the encoding type.

Power users can then set the form enctype and/or input formenctype precisely as required, without the auto-magic feature overriding explicitly set values.

@djyotta djyotta changed the base branch from daniel/435-spinner-to-indicate-page-is-loading to daniel/458-allow-set-formenctype June 27, 2024 00:45
djyotta and others added 3 commits June 27, 2024 12:48
Proposed changes for improved handling of form encoding types
…ess of form data types. Override formenctype on default submit button to use url encoded when there is no file present. Do not override dev specified enctype (i.e., there is a use case for file type input with url encoding if backend just processes file name).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant