# Text input

### Buttons that ask for text

Some requests need something only you know: a tracking number, a note, a name. Give that button `inputs`, up to 8. A press opens a form in the page's place, as Add Event's does (or [over the page](#a-form-over-the-page)), and the form's own button (`submit`, "Add" unless set) sends the request with what was typed. This is the Packages page's Add tracking, for Parcel's add-delivery request:

```json
{ "label": "Add tracking", "icon": "plus", "path": "/external/add-delivery/",
  "inputs": [
    { "name": "number", "label": "Tracking number", "required": true, "max": 40,
      "pattern": "[A-Z0-9]{8,40}", "spaces": "remove", "case": "upper",
      "hint": "As the carrier prints it", "invalid": "Letters and digits only" },
    { "name": "carrier", "label": "Carrier", "kind": "choice", "required": true,
      "choices": [{ "label": "USPS", "value": "usps", "icon": "si:usps" },
                  { "label": "UPS", "value": "ups", "icon": "si:ups" },
                  { "label": "FedEx", "value": "fedex", "icon": "si:fedex" }],
      "guess": { "from": "number", "rules": [{ "pattern": "1Z.*", "value": "ups" },
                                             { "pattern": "9[2-5][0-9]{20}", "value": "usps" }] } },
    { "name": "description", "label": "Description", "required": true, "max": 60, "placeholder": "What's in it" }
  ],
  "body": { "tracking_number": "{input.number}", "carrier_code": "{input.carrier}", "description": "{input.description}" },
  "success": { "path": "success", "equals": true }, "error": "error_message" }
```

| Input field | | |
| --- | --- | --- |
| `name` | required | A letter, then letters, digits or `_`. `{input.<name>}` fills it. |
| `label` | required | 1–24 characters, above the field. |
| `kind` | optional | `text` (the default), one line; `choice`, one of `choices`: chips when there are up to 6, a menu when there are more; or `toggle`, an on/off switch. |
| `on_value`, `off_value` | toggle | What a toggle sends when it's on and when it's off: a string, a number, true or false, or a list of up to 20 strings or numbers. Unless set, on is `true` and off is `false`; with an `on_value` and no `off_value`, the key is left out of the body when it's off. |
| `required` | optional | Add stays dimmed until every required field has something in it. |
| `placeholder`, `hint` | optional | Up to 40 characters. The hint is the line under the field, where a problem shows instead. |
| `max` | text | The longest value, 1–200 characters after `spaces` and `case`. 100 unless set. |
| `pattern` | text | A regular expression the whole value must match, such as `[A-Z0-9]{8,40}`. `invalid` (up to 80 characters) is what a value that misses it is told. |
| `spaces`, `case` | text | `"spaces": "remove"` takes every space out; `"case"` is `upper`, `lower` or `keep`. Both apply before the value is checked and sent, so a pasted `1z 999 aa1` goes out as `1Z999AA1`. |
| `default` | optional | A text input's starting text, or a choice's value. |
| `choices` | choice | 2–24 of `{ "label", "value", "icon" }`, the icon optional. |
| `guess` | choice | Picks a choice from a text input as it's typed: the first rule whose `pattern` matches all of it. A pick by hand stays. |
| `start` | optional | What the field opens on, read from the row (a row's button) or the card's answer, such as `"payee"`. See [Edit forms](#edit-forms). |
| `multiple` | choice | `true` takes several of the choices, as checkboxes, and sends them as a list. |
| `lines` | text | 2–8 lets the text run over lines, as notes do, growing to that many before it scrolls. |

- **Every field is checked before anything is sent.** Each one's problem shows once you leave it, or when you press Add, in the line under it. Return presses Add; Escape is Cancel. Some services count a refused request against a daily limit (Parcel allows 20 adds a day, refusals included), so a value Legato can tell is wrong never goes out.
- **The service says no.** With `success` and `error`, the service's own reason shows beside Cancel and Add, and the fields stay filled to fix and try again. Legato doesn't retry by itself.
- **It works:** the form closes and the source reads again, as after any button.
- **The form is kept** while you look at other pages or close the panel, until Add works or Cancel. Pressing the button again brings it back.
- **Typed text goes in as a value, never as part of the request's wording.** In the body it's a JSON string, escaped as JSON. A body string that is `"{input.x}"` and nothing else takes the value as it is, so a choice's number stays a number; when an optional field is left empty, that key is left out. Elsewhere in a string it's filled as text, and empty when the field is. Text holding `{secret}` or `{action.id}` arrives as typed: nothing typed is read for placeholders.
- **In the path, only checked values.** A choice can fill the path or a header, since its values are the page's own. A text input can fill the path only with a `pattern`, and it is percent-encoded, so `/`, `?`, `&` or `#` can't change the address. Typed text never fills a header.
- **The request is filled from the inputs** (and `{action.id}`, and any `{answer.…}`) only, as a menu's is from its choice.
- **Spotlight opens the form,** on the button's page, and nothing is sent until Add. **Shortcuts** presses it only when every required input has a `default`; otherwise it says to open the form. **It never goes on a notification.**
- A button with `inputs` can't also have `choices` or `confirm` (Add is the decision), and a checklist's `itemAction` can't have them.

### A form over the page

`"form_style": "card"` opens the form in a card that slides in over the page, under the right end of the title row where Details's card opens, so the page stays in view while you answer. Use it for a short answer about something the page shows, such as a category for one transaction from a row's button:

```json
{ "label": "Choose category", "icon": "pencil", "form_style": "card", "submit": "Save",
  "method": "PUT", "path": "/v2/transactions/{id}",
  "inputs": [ { "name": "category", "label": "Category", "kind": "choice", "required": true,
                "choices": [ { "label": "Groceries", "value": 12 }, { "label": "Household", "value": 31 } ] } ],
  "body": { "category_id": "{input.category}", "status": "reviewed" } }
```

- The card takes the keyboard, so you can type in its fields. Return presses the card's button, except in a long list's filter field, where it picks the highlighted choice without sending.
- The fields are grouped as System Settings groups them: a label and its value on each row, and a field that needs room, such as notes or several picks, in a group of its own under its label.
- Escape or Cancel puts the form away without sending anything. A click elsewhere in the panel does too while nothing has changed, and the click goes on to what it was on, so you can click from one row to the next. Once something has changed, a click elsewhere leaves the form open, so nothing you typed is lost. Clicking another app or the menu bar icon closes the panel as usual and keeps the form: the card is back when the panel opens on its page.
- On a row's button, the card is titled with the row, with the row's amount and grey line under it. A long list's suggestions lead under Suggested, with the search box over every choice below them.
- When the service says no, the card stays open with its reason, as the form in the page's place does.
- Everything else is the same as in the page's place. `"form_style": "page"` is the default, and only a button with `inputs` can have `form_style`.

### Edit forms

A form can open on what's there now and send back only what you change. Give each input a `start`, read from the row for a row's button or from the card's answer for a card's button, and the button `"send": "changed"`. This is the Transactions page's edit form, opened with a click on the row (`"row_actions_place": "row"`):

```json
{ "label": "Edit transaction", "icon": "pencil", "method": "PUT", "path": "/v2/transactions/{id}",
  "form_style": "card", "submit": "Save", "send": "changed",
  "inputs": [
    { "name": "category", "label": "Category", "kind": "choice", "start": "category_id",
      "choices": { "each": "cats.categories", "fields": { "label": "name", "value": "id", "group": "group_name" } } },
    { "name": "payee", "label": "Payee", "start": "payee", "max": 140 },
    { "name": "reviewed", "label": "Reviewed", "kind": "toggle", "start": "status",
      "on_value": "reviewed", "off_value": "unreviewed" },
    { "name": "notes", "label": "Notes", "lines": 4, "start": "notes", "max": 200 },
    { "name": "tags", "label": "Tags", "kind": "choice", "multiple": true, "start": "tag_ids",
      "choices": { "each": "tags.tags", "fields": { "label": "name", "value": "id" } } } ],
  "body": { "category_id": "{input.category}", "payee": "{input.payee}", "status": "{input.reviewed}",
            "notes": "{input.notes}", "tag_ids": "{input.tags}" } }
```

- A text field opens on the value as text, and a choice on the choice with that value. Several picks open on the values in a list that are also choices. A toggle opens on when the value is its `on_value` or `true`.
- With `"send": "changed"`, Save stays dimmed until something differs from what the form opened on. Only the changed fields go out: the other keys are left out of the body, so the form never writes back a value nobody touched. A field you empty on purpose goes as empty: `""` for text, `null` for a choice, `[]` for several picks.
- When the row changed at the service since the form opened, nothing is sent and the card says so.
- Leave out `send` (or write `"send": "all"`) when the request replaces the whole thing, such as Pi-hole's list of upstream servers: every field goes, its `start` only filling the form.

### Choices from the answer

A choice input can list what the card's answer holds instead of fixed choices, such as a budget's categories. Give `choices` a list rule, as a map's `items` takes, with `label`, `value` and, if you like, `group`:

```json
{ "name": "category", "label": "Category", "kind": "choice", "required": true,
  "choices": { "each": "cats.categories",
               "where": { "all": [ { "path": "archived", "equals": false }, { "path": "is_group", "equals": false } ] },
               "fields": { "label": "name", "value": "id", "group": "group_name" } } }
```

- The list is read when the form opens, from the answer the card shows, so the request that fills it goes in the card's source, beside the others. Up to 500 choices, in the answer's order; each group is a heading in the menu.
- When the form is sent, the answer is read again, and a value that has left the list since is refused.
- Past 24 choices the menu becomes a search box over a short list. Typing narrows it to the choices whose label holds what you type, the arrow keys move through what shows, and Return picks one without sending the form.
- A choice made from the answer can't have a `default` or a `guess`. If it’s required, open the form to pick a value. Shortcuts can omit an optional choice when it presses the button. Its values come from the service, so they can fill the path or the body, never a header.

On a row's button, `suggest` reads the row for the values worth offering first, such as the categories an agent thinks fit:

```json
"suggest": { "each": "custom_metadata.legato.choices", "fields": { "value": "category_id" } }
```

The suggestions show as chips above the menu, and the first one is picked when the form opens. Only values that are also on the input's list count, up to 6, and they take the list's labels, so a row can't add a choice or name one differently. Picking a chip or a menu entry sets the same value.

### A switch in the form

A `toggle` is on or off, so it's never `required`, and its `default` is `true` or `false` (off unless set). It fills one JSON body value whole, written exactly `"{input.name}"`: never the path, a header, a form field or part of a longer text, and a form that opens a page can't have one. Numbers in `on_value` and `off_value` must be whole numbers up to 9007199254740991, or have a fraction. This one adds a tag when it's on and leaves the tags alone when it's off:

```json
{ "name": "remember", "label": "Remember for this payee", "kind": "toggle", "on_value": [77] }
```

with `"additional_tag_ids": "{input.remember}"` in the body. A list or true/false filling a body value whole goes out as JSON.

### Forms that open a page

A form can open one of your pages with what was typed, instead of sending a request: a search. Give an `open` button `inputs`, and make its `url` a link to the page, with each `{input.name}` as a whole value of the link's query. This is the Paperless page's Search Paperless:

```json
{ "kind": "open", "label": "Search Paperless", "icon": "magnifyingglass",
  "url": "legato://page/paperless-search?q={input.q}",
  "inputs": [ { "name": "q", "label": "Search", "required": true, "max": 200, "placeholder": "Words, or tag:bills" } ],
  "submit": "Search" }
```

The page it opens takes the value as a [param](https://legatobar.com/docs/detail-pages.md#one-page-for-every-item) of type `query`, `"params": { "q": { "type": "query", "example": "insurance" } }`, and its source asks the service with it: `/api/documents/?query={param.q}&page_size=25`.

- **What was typed goes in the link as one value,** percent-encoded, so `tag:bills "late fee" & more` arrives as typed and can't add a name to the link or reach another page. The page checks it as its param, and when it refuses the value, the form stays open and says why.
- **Every input is required,** since a page link takes every value. The link names one of your pages, its id and names written out; only the values come from the form.
- **Search sends the form and shows the page,** as a card's link does: the form closes, and Back returns to the page you searched from. Nothing is sent to the button's own source.
- **It stays on a card with nothing to show,** as Add Event does: an empty inbox is still worth searching from.
- **Spotlight opens the form,** as for any button with `inputs`. It never goes on a notification.

