Skip to content

Commit 7492e2f

Browse files
Daniel SheffieldDaniel Sheffield
Daniel Sheffield
authored and
Daniel Sheffield
committed
update docs and add caution on formenctype behaviour for file type input
1 parent 4ded1d1 commit 7492e2f

File tree

3 files changed

+83
-7
lines changed

3 files changed

+83
-7
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
SET ":enctype" = CASE :percent_encoded IS NOT NULL OR :multipart_form_data IS NOT NULL
2+
WHEN TRUE THEN 'with ``' || COALESCE(:percent_encoded, :multipart_form_data) || '``'
3+
ELSE 'form'
4+
END ||' encoding type'
5+
SELECT 'text' AS component;
6+
SELECT 'The following data was submitted '||:enctype||':
7+
```
8+
' || :data ||'
9+
```' AS contents_md;

examples/official-site/sqlpage/migrations/01_documentation.sql

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,23 @@ INSERT INTO component(name, icon, description) VALUES
211211
'The value entered by the user in a field named x will be accessible to the target SQL page as a variable named $x.
212212
For instance, you can create a SQL page named "create_user.sql" that would contain "INSERT INTO users(name) VALUES($name)"
213213
and a form with its action property set to "create_user.sql" that would contain a field named "name".');
214+
INSERT INTO parameter(component, name, description_md, type, top_level, optional) SELECT 'form', * FROM (VALUES
215+
-- top level
216+
('enctype', '
217+
When ``method="post"``, this specifies how the form-data should be encoded
218+
when submitting it to the server.
219+
', 'TEXT', TRUE, TRUE),
220+
-- item level
221+
('formenctype', '
222+
When ``type`` is ``submit`` or ``image``, this specifies how the form-data
223+
should be encoded when submitting it to the server.
214224
225+
Takes precedence over any ``enctype`` set on the ``form`` element.
226+
227+
NOTE: when a ``file`` type input is present, then ``formenctype="multipart/form-data"``
228+
is automatically applied to the default validate button.
229+
', 'TEXT', FALSE, TRUE)
230+
);
215231
INSERT INTO parameter(component, name, description, type, top_level, optional) SELECT 'form', * FROM (VALUES
216232
-- top level
217233
('method', 'Set this to ''GET'' to pass the form contents directly as URL parameters. If the user enters a value v in a field named x, submitting the form will load target.sql?x=v. If target.sql contains SELECT $x, it will display the value v.', 'TEXT', TRUE, TRUE),
@@ -401,6 +417,15 @@ But note that SQLPage cookies already have the `SameSite=strict` attribute by de
401417
## File upload
402418
403419
You can use the `file` type to allow the user to upload a file.
420+
421+
> NOTE: It is recommended to use ``multipart/form-data`` to encode the from
422+
data for file uploads. This is because the default encoding
423+
(``application/x-www-form-urlencoded``) applied to arbitrary binary can be up
424+
to 3 times the size of the file.
425+
For now, ``formenctype="multipart/form-data"`` is automatically applied to the
426+
default validate button - but this may change in the future.
427+
428+
404429
The file will be uploaded to the server, and you will be able to access it using the
405430
[`sqlpage.uploaded_file_path`](functions.sql?function=uploaded_file_path#function) function.
406431
@@ -410,10 +435,55 @@ Here is how you could save the uploaded file to a table in the database:
410435
INSERT INTO uploaded_file(name, data) VALUES(:filename, sqlpage.uploaded_file_data_url(:filename))
411436
```
412437
',
413-
json('[{"component":"form", "title": "Upload a picture", "validate": "Upload", "action": "examples/handle_picture_upload.sql"},
438+
json('[{"component":"form", "enctype": "multipart/form-data", "title": "Upload a picture", "validate": "Upload", "action": "examples/handle_picture_upload.sql"},
414439
{"name": "my_file", "type": "file", "accept": "image/png, image/jpeg", "label": "Picture", "description": "Upload a small picture", "required": true}
415440
]')),
416441
('form', '
442+
## Form Encoding
443+
444+
You can specify the way form data should be encoded by setting the `enctype`
445+
top-level property on the form.
446+
447+
You may also specify `formenctype` on `submit` and `image` type inputs.
448+
This will take precedence over the `enctype` specified on the form and is
449+
useful in the case there are multiple `submit` buttons on the form.
450+
For example, an external site may have specific requirements on encoding type.
451+
452+
As a rule of thumb, ``multipart/form-data`` is best when fields may contain
453+
copious non-ascii characters or for binary data such as an image or a file.
454+
However, ``application/x-www-form-urlencoded`` creates less overhead when
455+
many short ascii text values are submitted.
456+
',
457+
json('[
458+
{
459+
"component": "form",
460+
"method": "post",
461+
"enctype": "multipart/form-data",
462+
"title": "Submit with different encoding types",
463+
"validate": "Submit with form encoding type",
464+
"action": "examples/handle_enctype.sql"
465+
},
466+
{"name": "data", "type": "text", "label": "Data", "required": true},
467+
{
468+
"name": "percent_encoded",
469+
"type": "submit",
470+
"label": "Submit as",
471+
"width": 4,
472+
"formaction": "examples/handle_enctype.sql",
473+
"formenctype": "application/x-www-form-urlencoded",
474+
"value": "application/x-www-form-urlencoded"
475+
},
476+
{
477+
"name": "multipart_form_data",
478+
"type": "submit",
479+
"label": "Submit as",
480+
"width": 4,
481+
"formaction": "examples/handle_enctype.sql",
482+
"formenctype": "multipart/form-data",
483+
"value": "multipart/form-data"
484+
}
485+
]')),
486+
('form', '
417487
## Bulk data insertion
418488
419489
You can use the `file` type to allow the user to upload a [CSV](https://en.wikipedia.org/wiki/Comma-separated_values)

sqlpage/templates/form.handlebars

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{{#if id}}id="{{id}}"{{/if}}
33
class="my-3 {{class}}"
44
method="{{default method "post"}}"
5+
{{#if enctype}}enctype="{{enctype}}"{{/if}}
56
{{#if action}}action="{{action}}"
67
{{else}}
78
{{#if id}}action="#{{id}}"{{/if}}
@@ -118,13 +119,9 @@
118119
</label>
119120
{{/if}}
120121
{{/if}}
121-
{{#if (eq type "file")}}
122-
<!-- Change the form encoding type if this is a file input-->
122+
{{#if eq type "file"}}
123+
<!-- Change the form encoding type if this is a file input -->
123124
{{#delay}}formenctype="multipart/form-data"{{/delay}}
124-
{{else}}
125-
{{#if (and formenctype (eq type "textarea"))}}
126-
{{#delay}} formenctype="{{formenctype}}"{{/delay}}
127-
{{/if}}
128125
{{/if}}
129126
{{/each_row}}
130127
</div>

0 commit comments

Comments
 (0)