Skip to content

Commit 7766a77

Browse files
committed
improve the form component documentation
1 parent 6dbe465 commit 7766a77

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

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

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -230,29 +230,34 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
230230
('max', 'The minimum value to accept for an input of type number', 'NUMBER', FALSE, TRUE),
231231
('step', 'The increment of values in an input of type number. Set to 1 to allow only integers.', 'NUMBER', FALSE, TRUE),
232232
('description', 'A helper text to display near the input field.', 'TEXT', FALSE, TRUE),
233+
('pattern', 'A regular expression that the value must match. For instance, [0-9]{3} will only accept 3 digits.', 'TEXT', FALSE, TRUE),
233234
('autofocus', 'Automatically focus the field when the page is loaded', 'BOOL', FALSE, TRUE)
234235
) x;
235236
INSERT INTO example(component, description, properties) VALUES
236237
(
237238
'form',
238239
'
239-
A form that asks the user for a parameter named `component`, and then posts the results to another page named `documentation.sql`.
240-
That file could contain a sql statement like
241-
```sql
242-
SELECT * FROM documentation WHERE component_name = $component
243-
```
244-
to display the documentation for the component the user selected.
245240
246-
Or it could contain a sql statement like
241+
The best way to manage forms in SQLPage is to create at least two separate files:
242+
243+
- one that will contain the form itself, and will be loaded when the user visits the page,
244+
- one that will handle the form submission, and will redirect to whatever page you want to display after the form has been submitted.
245+
246+
For instance, if you were creating a form to manage a list of users, you could create:
247+
248+
- a file named `users.sql` that would contain a list of users and a form to create a new user,
249+
- a file named `create_user.sql` that would insert the new user in the database, and then redirect to `users.sql`.
250+
251+
`create_user.sql` could contain a sql statement like
252+
247253
```sql
248-
INSERT INTO components(name) VALUES ($component)
254+
INSERT INTO users(name) VALUES(:username)
255+
RETURNING ''redirect'' AS component, ''users.sql'' AS link
249256
```
250257
251-
to allow users to create a new component.
252-
253-
When loading the page, the value for `$component` will be `NULL` if no value has been submitted.
258+
When loading the page, the value for `:username` will be `NULL` if no value has been submitted.
254259
',
255-
json('[{"component":"form", "action": "documentation.sql"}, {"name": "component"}]')),
260+
json('[{"component":"form", "action": "create_user.sql"}, {"name": "username"}]')),
256261
('form', 'A user registration form, illustrating the use of required fields, and different input types.',
257262
json('[{"component":"form", "title": "User", "validate": "Create new user"}, '||
258263
'{"name": "First name", "placeholder": "John"}, '||
@@ -279,7 +284,27 @@ FROM fruits
279284
', json('[{"component":"form"}, '||
280285
'{"name": "Fruit", "type": "select", "value": 1, "options": '||
281286
'"[{\"label\": \"Orange\", \"value\": 0}, {\"label\": \"Apple\", \"value\": 1}, {\"label\": \"Banana\", \"value\": 3}]"}
282-
]'));
287+
]')),
288+
('form', 'This example illustrates the use of the `radio` type.
289+
The `name` parameter is used to group the radio buttons together.
290+
The `value` parameter is used to set the value that will be submitted when the user selects the radio button.
291+
The `label` parameter is used to display a friendly name for the radio button.
292+
The `description` parameter is used to display a helper text near the radio button.
293+
294+
We could also save all the options in a database table, and then run a simple query like
295+
296+
```sql
297+
SELECT ''form'' AS component;
298+
SELECT * FROM fruit_option;
299+
```
300+
301+
In this example, depending on what the user clicks, the target `index.sql` page will be loaded with a the variable `$fruit` set to the string "1", "2", or "3".
302+
303+
', json('[{"component":"form", "method": "GET", "action": "index.sql"}, '||
304+
'{"name": "fruit", "type": "radio", "value": 1, "description": "An apple a day keeps the doctor away", "label": "Apple"}, '||
305+
'{"name": "fruit", "type": "radio", "value": 2, "description": "Oranges are a good source of vitamin C", "label": "Orange"}, '||
306+
'{"name": "fruit", "type": "radio", "value": 3, "description": "Bananas are a good source of potassium", "label": "Banana"}'||
307+
']'));
283308

284309
INSERT INTO component(name, icon, description) VALUES
285310
('chart', 'timeline', 'A component that plots data. Line, area, bar, and pie charts are all supported. Each item in the component is a data point in the graph.');

0 commit comments

Comments
 (0)