|
| 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: |2 |
| 20 | +
|
| 21 | + When ``method="post"``, this specifies how the form-data should be encoded |
| 22 | + when submitting it to the server. |
| 23 | + - name: id |
| 24 | + type: TEXT |
| 25 | + description: A unique identifier for the form, which can then be used to validate the form from a button |
| 26 | + outside of the form. |
| 27 | + - name: method |
| 28 | + type: TEXT |
| 29 | + description: Set this to 'GET' to pass the form contents directly as URL parameters. If the user enters |
| 30 | + a value v in a field named x, submitting the form will load target.sql?x=v. If target.sql contains SELECT |
| 31 | + $x, it will display the value v. |
| 32 | + - name: narrow |
| 33 | + type: BOOLEAN |
| 34 | + description: Whether to trim the horizontal padding between the icon and the label of the validation and |
| 35 | + reset buttons. |
| 36 | + - name: reset |
| 37 | + type: TEXT |
| 38 | + description: The text to display in the button at the bottom of the form that resets the form to its original |
| 39 | + state. Omit this property not to show a reset button at all. |
| 40 | + - name: reset_color |
| 41 | + type: COLOR |
| 42 | + description: The color of the button at the bottom of the form that resets the form to its original state. |
| 43 | + Omit this property to use the default color. |
| 44 | + - name: reset_icon |
| 45 | + type: ICON |
| 46 | + description: Name of an icon to be displayed on the left side of the reset button. |
| 47 | + - name: reset_outline |
| 48 | + type: COLOR |
| 49 | + description: A color to outline the reset button. |
| 50 | + - name: reset_shape |
| 51 | + type: TEXT |
| 52 | + description: The shape of the reset button (e.g., pill, square). |
| 53 | + - name: reset_size |
| 54 | + type: TEXT |
| 55 | + description: The size of the reset button (e.g., sm, lg). |
| 56 | + - name: title |
| 57 | + type: TEXT |
| 58 | + description: A name to display at the top of the form. It will be displayed in a larger font size at the |
| 59 | + top of the form. |
| 60 | + - name: validate |
| 61 | + type: TEXT |
| 62 | + description: The text to display in the button at the bottom of the form that submits the values. Omit |
| 63 | + this property to let the browser display the default form validation text, or set it to the empty string |
| 64 | + to remove the button completely. |
| 65 | + - name: validate_color |
| 66 | + type: COLOR |
| 67 | + description: The color of the button at the bottom of the form that submits the values. Omit this property |
| 68 | + to use the default color. |
| 69 | + - name: validate_icon |
| 70 | + type: ICON |
| 71 | + description: Name of an icon to be displayed on the left side of the submit button. |
| 72 | + - name: validate_outline |
| 73 | + type: COLOR |
| 74 | + description: A color to outline the validation button. |
| 75 | + - name: validate_shape |
| 76 | + type: TEXT |
| 77 | + description: The shape of the validation button (e.g., pill, square). |
| 78 | + - name: validate_size |
| 79 | + type: TEXT |
| 80 | + description: The size of the validation button (e.g., sm, lg). |
| 81 | +row: |
| 82 | + - name: accept |
| 83 | + type: TEXT |
| 84 | + description: Used only for inputs of type "file". A comma-separated list of the file types the user can |
| 85 | + pick, given as extensions (".pdf,.png") or as MIME types ("image/*"). |
| 86 | + - name: autocomplete |
| 87 | + type: BOOLEAN |
| 88 | + description: Whether the browser should suggest previously entered values for this field. |
| 89 | + - name: autofocus |
| 90 | + type: BOOLEAN |
| 91 | + description: Automatically focus the field when the page is loaded |
| 92 | + - name: checked |
| 93 | + type: BOOLEAN |
| 94 | + description: Used only for checkboxes and radio buttons. Indicates whether the checkbox should appear as |
| 95 | + already checked. |
| 96 | + - name: class |
| 97 | + type: TEXT |
| 98 | + description: A CSS class to apply to the form element. |
| 99 | + - name: create_new |
| 100 | + type: BOOLEAN |
| 101 | + description: In a select with a dropdown (`searchable`, `dropdown` or `options_source`), this option allows |
| 102 | + the user to enter new values, that are not in the list of options. Works for single selects and for multiselects. |
| 103 | + - name: description |
| 104 | + type: TEXT |
| 105 | + description: A helper text to display near the input field. |
| 106 | + - name: description_md |
| 107 | + type: TEXT |
| 108 | + description: A helper text to display near the input field - formatted using markdown. |
| 109 | + - name: disabled |
| 110 | + type: BOOLEAN |
| 111 | + description: Makes the field non-editable, non-focusable, and not submitted with the form. Use readonly |
| 112 | + instead for simple non-editable fields. |
| 113 | + - name: dropdown |
| 114 | + type: BOOLEAN |
| 115 | + description: An alias for "searchable". |
| 116 | + - name: empty_option |
| 117 | + type: TEXT |
| 118 | + description: Only for inputs of type `select`. Adds an empty option with the given label before the ones |
| 119 | + defined in `options`. Useful when generating other options from a database table. |
| 120 | + - name: formaction |
| 121 | + type: TEXT |
| 122 | + description: When type is "submit", this specifies the URL of the file that will handle the form submission. |
| 123 | + Useful when you need multiple submit buttons. |
| 124 | + - name: formenctype |
| 125 | + type: TEXT |
| 126 | + description: |2 |
| 127 | +
|
| 128 | + When ``type`` is ``submit`` or ``image``, this specifies how the form-data |
| 129 | + should be encoded when submitting it to the server. |
| 130 | +
|
| 131 | + Takes precedence over any ``enctype`` set on the ``form`` element. |
| 132 | +
|
| 133 | + NOTE: when a ``file`` type input is present, then ``formenctype="multipart/form-data"`` |
| 134 | + is automatically applied to the default validate button. |
| 135 | + - name: formmethod |
| 136 | + type: TEXT |
| 137 | + description: When type is "submit", the HTTP method to submit the form with. Takes precedence over the |
| 138 | + form's own "method". |
| 139 | + - name: formnovalidate |
| 140 | + type: BOOLEAN |
| 141 | + description: When type is "submit", skips the browser's validation of the other fields when this button |
| 142 | + submits the form. |
| 143 | + - name: formtarget |
| 144 | + type: TEXT |
| 145 | + description: 'When type is "submit", where to display the response. Takes the same values as a link target: |
| 146 | + "_blank" to open a new tab, "_self" to stay in the same one.' |
| 147 | + - name: id |
| 148 | + type: TEXT |
| 149 | + description: A unique identifier for the input, which can then be used to select and manage the field with |
| 150 | + Javascript code. Usefull for advanced using as setting client side event listeners, interactive control |
| 151 | + of input field (disabled, visibility, read only, e.g.) and AJAX requests. |
| 152 | + - name: label |
| 153 | + type: TEXT |
| 154 | + description: A friendly name for the text field to show to the user. |
| 155 | + - name: list |
| 156 | + type: TEXT |
| 157 | + description: The id of a datalist element holding the suggestions to show for this field. The datalist |
| 158 | + itself has to be rendered by the "html" component. |
| 159 | + - name: max |
| 160 | + type: REAL |
| 161 | + description: The maximum value to accept for an input of type number |
| 162 | + - name: maxlength |
| 163 | + type: INTEGER |
| 164 | + description: Maximum length of text allowed in the field. |
| 165 | + - name: min |
| 166 | + type: REAL |
| 167 | + description: The minimum value to accept for an input of type number |
| 168 | + - name: minlength |
| 169 | + type: INTEGER |
| 170 | + description: Minimum length of text allowed in the field. |
| 171 | + - name: multiple |
| 172 | + type: BOOLEAN |
| 173 | + description: 'For select elements, indicates that multiple elements can be selected simultaneously. When |
| 174 | + using multiple, you should add square brackets after the variable name: ''my_variable[]'' as name. For |
| 175 | + file inputs, lets the user select several files at once: save them all with [sqlpage.persist_uploaded_files](functions.sql?function=persist_uploaded_files).' |
| 176 | + - name: name |
| 177 | + type: TEXT |
| 178 | + required: true |
| 179 | + description: The name of the input field, that you can use in the target page to get the value the user |
| 180 | + entered for the field. |
| 181 | + - name: options |
| 182 | + type: JSON |
| 183 | + description: A json array of objects containing the label and value of initial options of a select field. |
| 184 | + Used only when type=select. JSON objects in the array can contain the properties "label", "value" and |
| 185 | + "selected". |
| 186 | + - name: options_source |
| 187 | + type: URL |
| 188 | + description: Only for inputs of type `select`. URL of a SQL file that returns JSON search results for the |
| 189 | + dropdown. The SQL file receives the search text as `$search` and must return an array of objects with |
| 190 | + exactly `label` and `value`. Use `options` for options that should be available before the user searches. |
| 191 | + - name: pattern |
| 192 | + type: TEXT |
| 193 | + description: A regular expression that the value must match. For instance, [0-9]{3} will only accept 3 |
| 194 | + digits. |
| 195 | + - name: placeholder |
| 196 | + type: TEXT |
| 197 | + description: A placeholder text that will be shown in the field when is is empty. |
| 198 | + - name: prefix |
| 199 | + type: TEXT |
| 200 | + description: Text to display on the left side of the input field, on the same line. |
| 201 | + - name: prefix_icon |
| 202 | + type: ICON |
| 203 | + description: Icon to display on the left side of the input field, on the same line. |
| 204 | + - name: readonly |
| 205 | + type: BOOLEAN |
| 206 | + description: Set to true to prevent the user from modifying the value of the input field. |
| 207 | + - name: required |
| 208 | + type: BOOLEAN |
| 209 | + description: Set this to true to prevent the form contents from being sent if this field is left empty |
| 210 | + by the user. |
| 211 | + - name: rows |
| 212 | + type: INTEGER |
| 213 | + description: Number of rows to display for a textarea. Defaults to 3. |
| 214 | + - name: searchable |
| 215 | + type: BOOLEAN |
| 216 | + description: For select and multiple-select elements, displays them with a nice dropdown that allows searching |
| 217 | + for options. |
| 218 | + - name: step |
| 219 | + type: REAL |
| 220 | + description: The increment of values in an input of type number. Set to 1 to allow only integers. |
| 221 | + - name: suffix |
| 222 | + type: TEXT |
| 223 | + description: Short text to display after th input, on the same line. Useful to add units or a currency |
| 224 | + symbol to an input. |
| 225 | + - name: type |
| 226 | + type: TEXT |
| 227 | + description: 'Declares input control behavior and expected format. All HTML input types are supported (text, |
| 228 | + number, date, file, checkbox, radio, hidden, ...). SQLPage adds some custom types: textarea, switch, |
| 229 | + header. text by default. See https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#input_types' |
| 230 | + - name: value |
| 231 | + type: TEXT |
| 232 | + description: A default value that will already be present in the field when the user loads the page. |
| 233 | + - name: width |
| 234 | + type: INTEGER |
| 235 | + description: Width of the form field, between 1 and 12. |
| 236 | +--- |
| 237 | + |
| 238 | +## Building forms in SQL |
| 239 | + |
| 240 | +The form component will display a series of input fields of various types, that can be filled in by the user. |
| 241 | +When the user submits the form, the data is posted to an SQL file specified in the `action` property. |
| 242 | + |
| 243 | +### Handling submitted values |
| 244 | + |
| 245 | +The receiving SQL page will be able to handle the data, |
| 246 | +and insert it into the database, use it to perform a search, format it, update existing data, etc. |
| 247 | + |
| 248 | +A value in a field named "x" will be available as `:x` in the SQL query of the target page. |
| 249 | + |
| 250 | +### Form projects |
| 251 | + |
| 252 | + - [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. |
| 253 | + - [File upload form](https://github.com/sqlpage/SQLPage/tree/main/examples/image%20gallery%20with%20user%20uploads), letting users upload images to a gallery. |
| 254 | + - [Rich text editor](https://github.com/sqlpage/SQLPage/tree/main/examples/rich-text-editor), letting users write text with bold, italics, links, images, etc. |
| 255 | + - [Master-detail form](https://github.com/sqlpage/SQLPage/tree/main/examples/master-detail-forms), to edit a list of structured items. |
| 256 | + - [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. |
| 257 | + - [Demo of all input types](/examples/form), showing all the input types supported by SQLPage. |
0 commit comments