Skip to content

Commit ec0e580

Browse files
committed
Form submission with button component. #113
1 parent 5dfd012 commit ec0e580

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Revert a change introduced in v0.15.0:
1111
- Re-add the systematic `CAST(? AS TEXT)` around variables, which helps the database know which type it is dealing with in advance. This fixes a regression in 0.15 where some SQLite websites were broken because of missing affinity information. In SQLite `SELECT '1' = 1` returns `false` but `SELECT CAST('1' AS TEXT) = 1` returns `true`.
1212
- Fix a bug where [cookie](https://sql.ophir.dev/documentation.sql?component=cookie#component) removal set the cookie value to the empty string instead of removing the cookie completely.
13+
- Support form submission using the [button](https://sql.ophir.dev/documentation.sql?component=button#component) component using its new `form` property. This allows you to create a form with multiple submit buttons that submit the form to different targets.
1314

1415
## 0.15.0 (2023-10-29)
1516
- New function: [`sqlpage.path`](https://sql.ophir.dev/functions.sql?function=path#function) to get the path of the current page.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
235235
('validate_color', 'The color of the button at the bottom of the form that submits the values. Omit this property to use the default color.', 'COLOR', TRUE, TRUE),
236236
('validate_outline', 'A color to outline the validation button.', 'COLOR', TRUE, TRUE),
237237
('reset', 'The text to display in the button at the bottom of the form that resets the form to its original state. Omit this property not to show a reset button at all.', 'TEXT', TRUE, TRUE),
238+
('id', 'A unique identifier for the form, which can then be used to validate the form from a button outside of the form.', 'TEXT', TRUE, TRUE),
238239
-- item level
239240
('type', 'The type of input to use: text for a simple text field, textarea for a multi-line text input control, number for field that accepts only numbers, checkbox or radio for a button that is part of a group specified in the ''name'' parameter. This is set to "text" by default.', 'TEXT', FALSE, TRUE),
240241
('name', 'The name of the input field, that you can use in the target page to get the value the user entered for the field.', 'TEXT', FALSE, FALSE),

examples/official-site/sqlpage/migrations/18_button.sql

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
1111
('size', 'The size of the buttons (e.g., sm, lg).', 'TEXT', TRUE, TRUE),
1212
('shape', 'Shape of the buttons (e.g., pill, square)', 'TEXT', TRUE, TRUE),
1313
-- Item-level parameters (for each button)
14-
('link', 'The URL to which the button should navigate when clicked.', 'URL', FALSE, TRUE),
14+
('link', 'The URL to which the button should navigate when clicked. If the form attribute is specified, then this overrides the page to which the form is submitted.', 'URL', FALSE, TRUE),
1515
('color', 'The color of the button (e.g., red, green, blue, but also primary, warning, danger, orange, etc.).', 'COLOR', FALSE, TRUE),
1616
('title', 'The text displayed on the button.', 'TEXT', FALSE, TRUE),
1717
('disabled', 'Whether the button is disabled or not.', 'BOOLEAN', FALSE, TRUE),
1818
('outline', 'Outline color of the button (e.g. red, purple, ...)', 'COLOR', FALSE, TRUE),
1919
('space_after', 'Whether there should be extra space to the right of the button. In a line of buttons, this will put the buttons before this one on the left, and the ones after on the right.', 'BOOLEAN', FALSE, TRUE),
20-
('icon', 'Name of an icon to be displayed on the left side of the button.', 'ICON', FALSE, TRUE)
20+
('icon', 'Name of an icon to be displayed on the left side of the button.', 'ICON', FALSE, TRUE),
21+
('form', 'Identifier (id) of the form to which the button should submit.', 'TEXT', FALSE, TRUE)
2122
) x;
2223

2324
-- Inserting example information for the button component
@@ -58,4 +59,19 @@ INSERT INTO example(component, description, properties) VALUES
5859
{"link":"#", "color":"green", "title":"Save" },
5960
{"link":"#", "color":"orange", "title":"Cancel", "space_after":true},
6061
{"link":"#", "outline":"indigo", "title":"Preview" }]')
62+
);
63+
64+
INSERT INTO example(component, description, properties) VALUES
65+
('button', 'Multiple buttons sending the same form to different pages.
66+
67+
We use `'''' AS validate` to remove the submit button from inside the form itself,
68+
and instead use the button component to submit the form to pages with different GET variables.
69+
70+
In the target page, we could then use the GET variable `$action` to determine what to do with the form data.
71+
',
72+
json('[{"component":"form", "id": "poem", "validate": ""},
73+
{"type": "textarea", "name": "Poem", "placeholder": "Write a poem"},
74+
{"component":"button"},
75+
{"link":"?action=save", "form":"poem", "color":"primary", "title":"Save" },
76+
{"link":"?action=preview", "form":"poem", "outline":"yellow", "title":"Preview" }]')
6177
);

sqlpage/templates/button.handlebars

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<div class="btn-list mb-2 {{#if justify}}justify-content-{{justify}}{{/if}}">
22
{{#each_row}}
3+
{{#if form}}
4+
<button type="submit" form="{{form}}" {{#if link}}formaction="{{link}}"{{/if}}
5+
{{else}}
36
<a href="{{link}}"
7+
{{/if}}
48
class="btn {{#if disabled}} disabled{{/if}}
59
{{~#if color}} btn-{{color}}{{/if}}
610
{{~#if ../size}} btn-{{../size}}{{/if}}
@@ -12,6 +16,10 @@
1216
<span class="me-1">{{~icon_img icon~}}</span>
1317
{{~/if~}}
1418
{{~title~}}
19+
{{#if form}}
20+
</button>
21+
{{else}}
1522
</a>
23+
{{/if}}
1624
{{/each_row}}
1725
</div>

sqlpage/templates/form.handlebars

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
<form class="my-3" method="{{default method "post"}}" {{#if action}}action="{{action}}"{{/if}}>
1+
<form
2+
{{#if id}}id="{{id}}"{{/if}}
3+
class="my-3"
4+
method="{{default method "post"}}"
5+
{{#if action}}action="{{action}}"{{/if}}
6+
>
27
<fieldset class="form-fieldset">
38
{{#if title}}
49
<h2 class="text-center mb-3">{{title}}</h2>

0 commit comments

Comments
 (0)