Skip to main content

Form Variables

Form variables turn a static query into an interactive one. Anywhere you write a $-prefixed variable in your SQL, Kvery renders an input field for it — so the same query can be re-run with different values without editing the SQL.

Declaring a variable​

Use a $-prefixed name (a dollar sign followed by a name) anywhere in your query:

SELECT *
FROM orders
WHERE status = '$status'
AND created_at >= '$from_date'
AND id IN ($ids);

Kvery detects $status, $from_date and $ids, shows an input for each, and substitutes the values you enter when the query runs. The values come from the request (the form fields, or the matching URL/body parameters when the query is called as an API).

Form variables vs. internal variables

A $-prefixed name ($status) is a form variable — it creates an input field and is filled from the request. A :-prefixed name (:log) is an internal variable that you declare or that Kvery provides; it is not a form field. (Note: :: — a double colon — is a PostgreSQL type cast, e.g. value::int, not a variable.)

Setting up the fields​

Below the SQL editor, every variable gets a settings card with the variable's name in its header: click the header to open the card. Expand all and Collapse all open or close every card, and dragging a card by its header changes the order of the fields.

Settings of every field​

  • Name — the label of the field; without one, the variable's name is shown. It can contain $name values and SQL (see below).
  • Swagger Description — shown when the query has the swagger tag: the description of the parameter in the Swagger documentation.
  • Type — the field type (Input text by default, see the table below).
  • Width — the width of the field on a 12-column grid (12 is the full width).
  • Hidden? — hides the field. Add condition adds a Show if: card with a Field, an Operator (equal, smaller than, greater than, smaller than or equal to, greater than or equal to, not equal, partially contain, not contain, fully contain) and a Value: the field appears only while the conditions are met. Conditions with the same Group number (1–99) are combined in their order with Logic within group (AND or OR with the next condition of the group), and the groups with Logic between groups (AND or OR with the next group). A condition on a field that is hidden itself counts as not met. Conditions Preview: shows the resulting rule.
  • Events enabled? — with Add event: If: the field's value meets a condition, Then: another field gets the given Value:. The value can use other variables without the $ sign, and simple arithmetic in it is calculated.
  • Automatically apply the value when it changes? — the form reloads with the new value as soon as it changes, so fields whose options or default value use this variable are updated.
  • Redirect value to parent window? (dashboard) — when the query runs in a dashboard and its form is sent (or a field with automatic apply changes), the dashboard page reloads with the value of this field as a URL parameter (several values joined with commas, an empty value left out). The dashboard passes its URL parameters to every query on it, so the other queries that use this variable get the value too.

A Label field has no Swagger Description, events, automatic apply or redirect to the parent window, and a Link field keeps only its Name and Type of these settings.

Field types​

TypeThe fieldIts own settings
Input textOne line of textPlaceholder, Default value, Mask, Hint, Required?, Readonly?
Input e-mail, Input password, Input telThe browser's e-mail, password (hidden characters) and phone number inputPlaceholder, Default value, Mask, Hint, Required?, Readonly?
Input URLThe browser's URL inputPlaceholder, Default value, Hint, Required?, Readonly? (the card also shows Mask, which the URL field does not use)
Input numberA numberPlaceholder, Default value, Minimum value, Maximum value, Step (1 by default), Hint, Required?, Readonly?
Input colorA colour pickerPlaceholder, Default value (a hexadecimal colour code such as #000000), Hint, Required?, Readonly?
TextareaSeveral lines of textPlaceholder, Default value, Maximum length, Number of rows (3 by default), Hint, HTML editor?, Required?, Readonly?
CheckboxA list of checkboxes; several can be tickedDefault value, Hint, Layout (Horizontal or Vertical), Is it mandatory to select at least one field?, options
RadioA list of radio buttons; one can be chosenThe same as Checkbox
SelectA drop-down list with a search boxDefault value, Hint, Required?, Add "Please select" option?, Multiselect?, options
DateA date picker (YYYY-MM-DD)Placeholder, Default value, Hint, Required?, Readonly?
TimeA time picker (HH:MM)Placeholder, Default value, Time step (minutes, 30 by default), Hint, Required?, Readonly?
Date and TimeA date and time picker (YYYY-MM-DD HH:MM:00)The same as Time
LabelIts Name as text, without an input—
LinkNo form field: a link in the result (see Links)Link color, Joined query, Type, URL variables, CSS classes

What the settings do:

  • Placeholder — the grey text of the empty field; without one, the Name is used.
  • Default value — the value of the field when the request has none (see Default values).
  • Mask — a regular expression: the field accepts only input that matches it (for example [0-9]{4} for four digits).
  • Hint — a question mark next to the label shows this text as a tooltip.
  • HTML editor? — turns the text area into a rich text editor.
  • Required? — the field is marked with a red *, and the form is not sent while it is empty ("All fields marked with * are required!"). For checkboxes and radio buttons, Is it mandatory to select at least one field? does the same.
  • Readonly? — the value is shown but cannot be changed.
  • Minimum value, Maximum value, Step — the limits and the step of a number; Maximum length limits the characters of a text area.
  • Add "Please select" option? — adds an empty first option to a select; its text is Option name ("Please select" when left empty).
  • Multiselect? — several options of the select can be chosen.

Default values​

A default value can be:

  • Text, in which $name is replaced with the value of that variable in the request's URL parameters: apostrophes, double quotes and backslashes of the value get a backslash in front of them, and runs of white space become a single space. When the URL has no value of that name, $name becomes the text NULL.
  • A SELECT (a value that starts with the word select): it runs on the query's connection, and the field gets the value of the selected column from the first row (select one column). When the SELECT uses a $name that the request does not have, the field stays empty. When the SELECT fails, the run stops with its error message and status (people who cannot edit the query get "Something went wrong!", unless the SELECT sets its own message with ON ERROR RETURN).

For Checkbox, Radio and Select fields, separate several values with commas (for example 1,3): the options with these values are checked or selected.

Options of checkboxes, radio buttons and selects​

  • Manual list (the default of List from query) — Add option adds an option with a Name (its label) and a Value. Checked? (checkbox, radio) or Selected? (select) preselects the option when the form opens without values; Disabled? (checkbox, radio) shows it greyed out. Drag the options to change their order.
  • List from query — another saved query gives the options: its value column is the value and its name column the label of each option ("The query must contain: value, name!"). For a select, a keywords column adds words the search box finds the option by; for checkboxes and radio buttons, a disabled column greys out the options where its value is true (not empty and not 0). The $name variables of that query are filled from the request's URL parameters, so the options can depend on other fields. When that query fails, the field has no options. The list query runs with the snippets of its own group. It has to be a query of the same group, or a query of another group that the creator of the form's query may share there (as its owner, an Editor, or a Manager whose member tags allow it), for example after cloning the query into another group; a query of any other group gives no options. Saving a form that names a query of another group needs the same right from the user who saves it.

A Link field is not an input. Where $name of the link field appears in a value of the query's result, the result shows a link with the field's Name as its text. Placed in a column name, the link appears only while rows of a selectable:: column are selected, and it passes the selected rows to the joined query (see Column modifiers):

  • Joined query — the query the link opens.
  • Type — Modal opens it in a window over the page, New tab in a new browser tab.
  • URL variables — the parameters passed to the joined query, either as k=v&k2=v2, where each value is a column name of the result, or as a comma-separated list of column names (each passed under its own name): every row passes its own values. A column that is missing or empty passes the written text itself.
  • Link color (used when no CSS classes are given) and CSS classes (Bootstrap 4 classes) — the look of the link.

How values are inserted​

The value replaces its parameter in the SQL text:

  • Every value in the query's own SQL is prepared the same way, wherever its parameter stands: surrounding white space is removed,   becomes a space, an empty value becomes NULL ('$status' with an empty value becomes NULL), and apostrophes are escaped (on MySQL and MariaDB backslashes too). The query runs as one line (see How Kvery runs the text), so the line breaks of a value are removed and runs of white space in it become a single space.

  • Inside quotes ('$status'; on MySQL and MariaDB also "$status", where the double quotes of the value are escaped as well) this escaping keeps the value inside the quoted text. Put text, dates and anything else that is not a plain number in quotes.

  • Without quotes ($id, IN ($ids), ORDER BY $column) nothing else is checked: the escaped value becomes part of the SQL code. Use this only for numbers, comma-separated lists of numbers and column names.

  • In a snippet, in the query of a List from query field and in a field's own SQL (a default value or a label) a value is inserted without trimming, and an empty value stays an empty text; in a snippet and in the query of a List from query field a name the request does not have becomes NULL. Apostrophes are escaped there too (on MySQL and MariaDB with a backslash, which double quotes and backslashes also get).

  • A value may not contain Kvery's own query syntax. A value that contains any of these texts, in any letter case, is refused — also when a line break splits the text:

    • the block and comment markers {%, %}, [%, %], /* and */;
    • snippet(, return( and returnk(;
    • on error return ';
    • an HTTP call: GET(', POST(', PUT(', PATCH(' or DELETE(';
    • the beginning of a webhook directive, such as on success POST ' or on error GET '.

    So ordinary data such as */5 * * * * (a cron expression) or [50%] is refused as well. A value that forms such syntax together with the SQL next to its parameter (for example a value ending in { right before a % of the query) is refused the same way. Such a value stops the run with HTTP status 400 and the message "A parameter value is not allowed: it contains syntax of the query engine (statement blocks, comment markers, snippet() or a directive such as return() or GET('...')).". In a field's own SQL (a default value or a label), in the query of a List from query field and in a snippet such a value is used as NULL; when the values together with the text around them form such syntax there, every value of that text is used as NULL.

The same rules apply in the editor, on shared links, in the API, through the MCP server, in imports and in scheduled runs.

A rich text field (a text area with the editor turned on) shows its value with its formatting. When the form shows a value from the link, from the previous run or the field's default value, its formatting, links, images, tables, inline styles, IDs and frames of https:// pages are kept; scripts, event handlers, script links, <style> blocks, form controls and frames of other addresses are removed, and its HTML is tidied (for example, a link that opens in a new tab gets rel="noreferrer noopener"), so a resubmitted form sends the tidied HTML.

A field's label can contain form variables ($name) and SQL (a label that starts with select or contains {% %} / [% %] blocks). The values come from the request's URL parameters. The inserted form variable values are shown as plain text: HTML in them is not interpreted. A $name the URL has no value for appears as NULL, and a label whose SQL uses such a $name stays empty. The SQL results are shown as HTML, the same way as a rich text value: line breaks, links and formatting work, scripts, event handlers and script links are removed. HTML that you write in the label yourself is kept. When the SQL of a label fails, the run stops with its error message, as for a default value.

Why use them​

  • Reusable reports. One query serves many cases — pick a date range, a status, a customer id.
  • Safe sharing. When you share or publish a query as an API, the form variables become the inputs/parameters the consumer supplies.
  • Dashboards. A dashboard tile can prompt for the variable so viewers drive the report themselves.

Using variables in the API​

When a query with form variables is published as an API endpoint, each variable becomes a request parameter. The caller passes values and the query uses them exactly as the form does in the editor. On API calls the request headers are values too, so a header can fill a variable of its name (see Request headers as values); treat such values as untrusted input like any other.

Tips​

  • Give variables clear names — they become field labels and API parameter names.
  • Combine with internal variables — for example :log[executed_by_email], the e-mail address of the user who runs the query — to scope results automatically.
  • Provide sensible defaults in your WHERE logic so the query still returns something useful before the user changes a value.