Skip to content

Commit 032109d

Browse files
authored
Merge pull request #459 from djyotta/daniel/458-allow-set-formenctype
#458 - allow user to set formenctype
2 parents 16d33ce + 96636f2 commit 032109d

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
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: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,23 @@ INSERT INTO component(name, icon, description) VALUES
212212
'The value entered by the user in a field named x will be accessible to the target SQL page as a variable named $x.
213213
For instance, you can create a SQL page named "create_user.sql" that would contain "INSERT INTO users(name) VALUES($name)"
214214
and a form with its action property set to "create_user.sql" that would contain a field named "name".');
215+
INSERT INTO parameter(component, name, description_md, type, top_level, optional) SELECT 'form', * FROM (VALUES
216+
-- top level
217+
('enctype', '
218+
When ``method="post"``, this specifies how the form-data should be encoded
219+
when submitting it to the server.
220+
', 'TEXT', TRUE, TRUE),
221+
-- item level
222+
('formenctype', '
223+
When ``type`` is ``submit`` or ``image``, this specifies how the form-data
224+
should be encoded when submitting it to the server.
225+
226+
Takes precedence over any ``enctype`` set on the ``form`` element.
215227
228+
NOTE: when a ``file`` type input is present, then ``formenctype="multipart/form-data"``
229+
is automatically applied to the default validate button.
230+
', 'TEXT', FALSE, TRUE)
231+
);
216232
INSERT INTO parameter(component, name, description, type, top_level, optional) SELECT 'form', * FROM (VALUES
217233
-- top level
218234
('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),
@@ -402,6 +418,7 @@ But note that SQLPage cookies already have the `SameSite=strict` attribute by de
402418
## File upload
403419
404420
You can use the `file` type to allow the user to upload a file.
421+
405422
The file will be uploaded to the server, and you will be able to access it using the
406423
[`sqlpage.uploaded_file_path`](functions.sql?function=uploaded_file_path#function) function.
407424
@@ -411,10 +428,55 @@ Here is how you could save the uploaded file to a table in the database:
411428
INSERT INTO uploaded_file(name, data) VALUES(:filename, sqlpage.uploaded_file_data_url(:filename))
412429
```
413430
',
414-
json('[{"component":"form", "title": "Upload a picture", "validate": "Upload", "action": "examples/handle_picture_upload.sql"},
431+
json('[{"component":"form", "enctype": "multipart/form-data", "title": "Upload a picture", "validate": "Upload", "action": "examples/handle_picture_upload.sql"},
415432
{"name": "my_file", "type": "file", "accept": "image/png, image/jpeg", "label": "Picture", "description": "Upload a small picture", "required": true}
416433
]')),
417434
('form', '
435+
## Form Encoding
436+
437+
You can specify the way form data should be encoded by setting the `enctype`
438+
top-level property on the form.
439+
440+
You may also specify `formenctype` on `submit` and `image` type inputs.
441+
This will take precedence over the `enctype` specified on the form and is
442+
useful in the case there are multiple `submit` buttons on the form.
443+
For example, an external site may have specific requirements on encoding type.
444+
445+
As a rule of thumb, ``multipart/form-data`` is best when fields may contain
446+
copious non-ascii characters or for binary data such as an image or a file.
447+
However, ``application/x-www-form-urlencoded`` creates less overhead when
448+
many short ascii text values are submitted.
449+
',
450+
json('[
451+
{
452+
"component": "form",
453+
"method": "post",
454+
"enctype": "multipart/form-data",
455+
"title": "Submit with different encoding types",
456+
"validate": "Submit with form encoding type",
457+
"action": "examples/handle_enctype.sql"
458+
},
459+
{"name": "data", "type": "text", "label": "Data", "required": true},
460+
{
461+
"name": "percent_encoded",
462+
"type": "submit",
463+
"label": "Submit as",
464+
"width": 4,
465+
"formaction": "examples/handle_enctype.sql",
466+
"formenctype": "application/x-www-form-urlencoded",
467+
"value": "application/x-www-form-urlencoded"
468+
},
469+
{
470+
"name": "multipart_form_data",
471+
"type": "submit",
472+
"label": "Submit as",
473+
"width": 4,
474+
"formaction": "examples/handle_enctype.sql",
475+
"formenctype": "multipart/form-data",
476+
"value": "multipart/form-data"
477+
}
478+
]')),
479+
('form', '
418480
## Bulk data insertion
419481
420482
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: 1 addition & 0 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}}

0 commit comments

Comments
 (0)