Skip to content

Commit 28209d0

Browse files
committed
Move the form reference and basic examples to Markdown
1 parent 8f4c74c commit 28209d0

9 files changed

Lines changed: 353 additions & 158 deletions

File tree

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
---
2+
icon: cursor-text
3+
category: forms-and-feedback
4+
summary: Collect input with text fields, file uploads, and other controls.
5+
top:
6+
- name: action
7+
type: TEXT
8+
description: An optional link to a target page that will handle the results of the form. By default the
9+
target page is the current page with the id of the form (if passed) used as hash - this will bring us
10+
back to the location of the form after submission. Setting it to the name of a different sql file will
11+
load that file when the user submits the form.
12+
- name: auto_submit
13+
type: BOOLEAN
14+
description: Automatically submit the form when the user changes any of its fields, and remove the validation
15+
button.
16+
- class
17+
- name: enctype
18+
type: TEXT
19+
description: Encoding used for POST submissions. Use `multipart/form-data` for file uploads.
20+
- name: id
21+
type: TEXT
22+
description: A unique identifier for the form, which can then be used to validate the form from a button
23+
outside of the form.
24+
- name: method
25+
type: TEXT
26+
description: >-
27+
The default is `POST`: read a field named `x` as `:x` on the target page.
28+
Use `GET` for bookmarkable searches and filters; the field appears in the URL as `?x=value` and can be read as `$x`.
29+
- name: narrow
30+
type: BOOLEAN
31+
description: Whether to trim the horizontal padding between the icon and the label of the validation and
32+
reset buttons.
33+
- name: reset
34+
type: TEXT
35+
description: The text to display in the button at the bottom of the form that resets the form to its original
36+
state. Omit this property not to show a reset button at all.
37+
- name: reset_color
38+
type: COLOR
39+
description: The color of the button at the bottom of the form that resets the form to its original state.
40+
Omit this property to use the default color.
41+
- name: reset_icon
42+
type: ICON
43+
description: Name of an icon to be displayed on the left side of the reset button.
44+
- name: reset_outline
45+
type: COLOR
46+
description: A color to outline the reset button.
47+
- name: reset_shape
48+
type: TEXT
49+
description: The shape of the reset button (e.g., pill, square).
50+
- name: reset_size
51+
type: TEXT
52+
description: The size of the reset button (e.g., sm, lg).
53+
- name: title
54+
type: TEXT
55+
description: A name to display at the top of the form. It will be displayed in a larger font size at the
56+
top of the form.
57+
- name: validate
58+
type: TEXT
59+
description: The text to display in the button at the bottom of the form that submits the values. Omit
60+
this property to let the browser display the default form validation text, or set it to the empty string
61+
to remove the button completely.
62+
- name: validate_color
63+
type: COLOR
64+
description: The color of the button at the bottom of the form that submits the values. Omit this property
65+
to use the default color.
66+
- name: validate_icon
67+
type: ICON
68+
description: Name of an icon to be displayed on the left side of the submit button.
69+
- name: validate_outline
70+
type: COLOR
71+
description: A color to outline the validation button.
72+
- name: validate_shape
73+
type: TEXT
74+
description: The shape of the validation button (e.g., pill, square).
75+
- name: validate_size
76+
type: TEXT
77+
description: The size of the validation button (e.g., sm, lg).
78+
row:
79+
- name: accept
80+
type: TEXT
81+
description: Used only for inputs of type "file". A comma-separated list of the file types the user can
82+
pick, given as extensions (".pdf,.png") or as MIME types ("image/*").
83+
- name: autocomplete
84+
type: BOOLEAN
85+
description: Whether the browser should suggest previously entered values for this field.
86+
- name: autofocus
87+
type: BOOLEAN
88+
description: Automatically focus the field when the page is loaded
89+
- name: checked
90+
type: BOOLEAN
91+
description: Used only for checkboxes and radio buttons. Indicates whether the checkbox should appear as
92+
already checked.
93+
- name: class
94+
type: TEXT
95+
description: A CSS class to apply to the form element.
96+
- name: create_new
97+
type: BOOLEAN
98+
description: In a select with a dropdown (`searchable`, `dropdown` or `options_source`), this option allows
99+
the user to enter new values, that are not in the list of options. Works for single selects and for multiselects.
100+
- name: description
101+
type: TEXT
102+
description: A helper text to display near the input field.
103+
- name: description_md
104+
type: TEXT
105+
description: A helper text to display near the input field - formatted using markdown.
106+
- name: disabled
107+
type: BOOLEAN
108+
description: Makes the field non-editable, non-focusable, and not submitted with the form. Use readonly
109+
instead for simple non-editable fields.
110+
- name: dropdown
111+
type: BOOLEAN
112+
description: An alias for "searchable".
113+
- name: empty_option
114+
type: TEXT
115+
description: Only for inputs of type `select`. Adds an empty option with the given label before the ones
116+
defined in `options`. Useful when generating other options from a database table.
117+
- name: formaction
118+
type: TEXT
119+
description: When type is "submit", this specifies the URL of the file that will handle the form submission.
120+
Useful when you need multiple submit buttons.
121+
- name: formenctype
122+
type: TEXT
123+
description: |2
124+
125+
When ``type`` is ``submit`` or ``image``, this specifies how the form-data
126+
should be encoded when submitting it to the server.
127+
128+
Takes precedence over any ``enctype`` set on the ``form`` element.
129+
130+
NOTE: when a ``file`` type input is present, then ``formenctype="multipart/form-data"``
131+
is automatically applied to the default validate button.
132+
- name: formmethod
133+
type: TEXT
134+
description: When type is "submit", the HTTP method to submit the form with. Takes precedence over the
135+
form's own "method".
136+
- name: formnovalidate
137+
type: BOOLEAN
138+
description: When type is "submit", skips the browser's validation of the other fields when this button
139+
submits the form.
140+
- name: formtarget
141+
type: TEXT
142+
description: 'When type is "submit", where to display the response. Takes the same values as a link target:
143+
"_blank" to open a new tab, "_self" to stay in the same one.'
144+
- name: id
145+
type: TEXT
146+
description: A unique identifier for the input, which can then be used to select and manage the field with
147+
Javascript code. Usefull for advanced using as setting client side event listeners, interactive control
148+
of input field (disabled, visibility, read only, e.g.) and AJAX requests.
149+
- name: label
150+
type: TEXT
151+
description: A friendly name for the text field to show to the user.
152+
- name: list
153+
type: TEXT
154+
description: The id of a datalist element holding the suggestions to show for this field. The datalist
155+
itself has to be rendered by the "html" component.
156+
- name: max
157+
type: REAL
158+
description: The maximum value to accept for an input of type number
159+
- name: maxlength
160+
type: INTEGER
161+
description: Maximum length of text allowed in the field.
162+
- name: min
163+
type: REAL
164+
description: The minimum value to accept for an input of type number
165+
- name: minlength
166+
type: INTEGER
167+
description: Minimum length of text allowed in the field.
168+
- name: multiple
169+
type: BOOLEAN
170+
description: 'For select elements, indicates that multiple elements can be selected simultaneously. When
171+
using multiple, you should add square brackets after the variable name: ''my_variable[]'' as name. For
172+
file inputs, lets the user select several files at once: save them all with [sqlpage.persist_uploaded_files](functions.sql?function=persist_uploaded_files).'
173+
- name: name
174+
type: TEXT
175+
required: true
176+
description: The name of the input field, that you can use in the target page to get the value the user
177+
entered for the field.
178+
- name: options
179+
type: JSON
180+
description: A json array of objects containing the label and value of initial options of a select field.
181+
Used only when type=select. JSON objects in the array can contain the properties "label", "value" and
182+
"selected".
183+
- name: options_source
184+
type: URL
185+
description: Only for inputs of type `select`. URL of a SQL file that returns JSON search results for the
186+
dropdown. The SQL file receives the search text as `$search` and must return an array of objects with
187+
exactly `label` and `value`. Use `options` for options that should be available before the user searches.
188+
- name: pattern
189+
type: TEXT
190+
description: A regular expression that the value must match. For instance, [0-9]{3} will only accept 3
191+
digits.
192+
- name: placeholder
193+
type: TEXT
194+
description: A placeholder text that will be shown in the field when is is empty.
195+
- name: prefix
196+
type: TEXT
197+
description: Text to display on the left side of the input field, on the same line.
198+
- name: prefix_icon
199+
type: ICON
200+
description: Icon to display on the left side of the input field, on the same line.
201+
- name: readonly
202+
type: BOOLEAN
203+
description: Set to true to prevent the user from modifying the value of the input field.
204+
- name: required
205+
type: BOOLEAN
206+
description: Set this to true to prevent the form contents from being sent if this field is left empty
207+
by the user.
208+
- name: rows
209+
type: INTEGER
210+
description: Number of rows to display for a textarea. Defaults to 3.
211+
- name: searchable
212+
type: BOOLEAN
213+
description: For select and multiple-select elements, displays them with a nice dropdown that allows searching
214+
for options.
215+
- name: step
216+
type: REAL
217+
description: The increment of values in an input of type number. Set to 1 to allow only integers.
218+
- name: suffix
219+
type: TEXT
220+
description: Short text to display after th input, on the same line. Useful to add units or a currency
221+
symbol to an input.
222+
- name: type
223+
type: TEXT
224+
description: 'Declares input control behavior and expected format. All HTML input types are supported (text,
225+
number, date, file, checkbox, radio, hidden, ...). SQLPage adds some custom types: textarea, switch,
226+
header. text by default. See https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#input_types'
227+
- name: value
228+
type: TEXT
229+
description: A default value that will already be present in the field when the user loads the page.
230+
- name: width
231+
type: INTEGER
232+
description: Width of the form field, between 1 and 12.
233+
---
234+
235+
## Building forms in SQL
236+
237+
The form component will display a series of input fields of various types, that can be filled in by the user.
238+
When the user submits the form, the data is posted to an SQL file specified in the `action` property.
239+
240+
### Handling submitted values
241+
242+
The receiving SQL page will be able to handle the data,
243+
and insert it into the database, use it to perform a search, format it, update existing data, etc.
244+
245+
A value in a field named "x" will be available as `:x` in the SQL query of the target page.
246+
247+
### Form projects
248+
249+
- [A multi-step form](https://github.com/sqlpage/SQLPage/tree/main/examples/forms-with-multiple-steps), guiding the user through a process without overwhelming them with a large form.
250+
- [File upload form](https://github.com/sqlpage/SQLPage/tree/main/examples/image%20gallery%20with%20user%20uploads), letting users upload images to a gallery.
251+
- [Rich text editor](https://github.com/sqlpage/SQLPage/tree/main/examples/rich-text-editor), letting users write text with bold, italics, links, images, etc.
252+
- [Master-detail form](https://github.com/sqlpage/SQLPage/tree/main/examples/master-detail-forms), to edit a list of structured items.
253+
- [Form with a variable number of fields](https://github.com/sqlpage/SQLPage/tree/main/examples/forms%20with%20a%20variable%20number%20of%20fields), when the fields are not known in advance.
254+
- [Demo of all input types](/examples/form), showing all the input types supported by SQLPage.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
rows:
3+
- component: form
4+
action: create_user.sql
5+
- name: username
6+
---
7+
8+
The best way to manage forms in SQLPage is to create at least two separate files:
9+
10+
- one that will contain the form itself, and will be loaded when the user visits the page,
11+
- one that will handle the form submission, and will redirect to whatever page you want to display after the form has been submitted.
12+
13+
For instance, if you were creating a form to manage a list of users, you could create:
14+
15+
- a file named `users.sql` that would contain a list of users and a form to create a new user,
16+
- a file named `create_user.sql` that would insert the new user in the database, and then redirect to `users.sql`.
17+
18+
`create_user.sql` could contain the following sql statement to [safely](safety.sql) insert the new user in the database:
19+
20+
```sql
21+
INSERT INTO users(name) VALUES(:username)
22+
RETURNING 'redirect' AS component, 'users.sql' AS link
23+
```
24+
25+
When loading the page, the value for `:username` will be `NULL` if no value has been submitted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
rows:
3+
- component: form
4+
title: User
5+
validate: Create new user
6+
- name: First name
7+
placeholder: John
8+
- name: Last name
9+
required: true
10+
description: We need your last name for legal purposes.
11+
- name: Resume
12+
type: textarea
13+
- name: Birth date
14+
type: date
15+
max: '2010-01-01'
16+
value: '1994-04-16'
17+
- name: Password
18+
type: password
19+
pattern: ^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$
20+
required: true
21+
description_md: '**Password Requirements:** Minimum **8 characters**, at least **one letter** & **one number**.
22+
*Tip:* Use a passphrase for better security!'
23+
- label: I accept the terms and conditions
24+
name: terms
25+
type: checkbox
26+
required: true
27+
---
28+
29+
A user registration form, illustrating the use of required fields, and different input types.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
rows:
3+
- component: form
4+
- name: Your account
5+
prefix_icon: mail
6+
prefix: 'Email:'
7+
suffix: '@mydomain.com'
8+
---
9+
10+
Create prepended and appended inputs to make your forms easier to use.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
rows:
3+
- component: form
4+
title: Information about the person
5+
- type: header
6+
label: Identity
7+
- name: Name
8+
- name: Surname
9+
- type: header
10+
label: Contact
11+
- name: phone
12+
label: Phone number
13+
- name: Email
14+
---
15+
16+
With the header type, you can group your input fields based on a theme. For example, you can categorize fields according to a person's identity and their contact information.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
rows:
3+
- component: form
4+
- type: switch
5+
label: Dark theme
6+
name: dark
7+
description: Enable dark theme
8+
- type: switch
9+
label: A required toggle switch
10+
name: my_checkbox
11+
required: true
12+
checked: true
13+
- type: switch
14+
label: A disabled toggle switch
15+
name: my_field
16+
disabled: true
17+
---
18+
19+
A toggle switch in an HTML form is a user interface element that allows users to switch between two states, typically "on" and "off." It visually resembles a physical switch and is often used for settings or options that can be enabled or disabled.

0 commit comments

Comments
 (0)