Forms & Input
Text entry in PAM Desktop uses the platform’s own controls: a GTK entry, an NSTextField, a Win32 EDIT control. Typing, selection, the clipboard and input methods are native, and masks are applied by the host as the user types, with no round trip to PHP. PHP receives the text through events and keeps it in state. For validation, NativeForm turns a plain class with attributes into a form with errors, dirty tracking and drafts.
Inputs
Section titled “Inputs”use Pam\Desktop\KeyboardType;use Pam\Desktop\UI\Input;
Input::make($this->email) ->keyboard(KeyboardType::Email) ->onChange(fn (string $value) => $this->email = $value) ->onSubmit($this->save(...)) ->style(new Style(height: 44, paddingHorizontal: 12, borderRadius: 10, borderWidth: 1, borderColor: 0xFFD1D5DB));In a template, model binds an input to a property in both directions:
| Method | Meaning |
|---|---|
multiline() |
A multi-line editor instead of a single-line field. |
secure() |
Password entry. |
maxLength(int) |
Longest text accepted. |
autoFocus() |
Takes keyboard focus when it appears. |
editable(false) |
Read-only: focusable and selectable, never modified. |
keyboard(KeyboardType) |
What the field is for: Text, Email, Number, Phone, Decimal, Url. |
onFocus, onBlur, onSubmit |
Focus changes and Enter. |
mask(pattern) |
Formats while typing; # stands for a digit. |
currency($decimalDigits, $prefix, $suffix, $locale) |
Formats an amount with a prefix, a suffix and a locale. |
decimal($minimumFractionDigits, $maximumFractionDigits, $prefix, $suffix, $locale) |
A decimal in the locale’s separators, up to 6 fraction digits; PHP receives an ASCII number such as 0.5. |
TextInput::make() is an alias of Input::make(). Without a width, a single-line input is as wide as a browser’s text field (20 average characters; inputSize in templates is HTML size).
Masks and formatted values
Section titled “Masks and formatted values”Input::make($this->phone) ->placeholder('(555) 123-4567') ->mask('(###) ###-####') ->onChange(fn (string $value) => $this->phone = $value);
Input::make($this->amount) ->currency(prefix: '$ ', locale: 'en-US') ->onChange(fn (string $value) => $this->amount = $value);The host formats on every keystroke. The text never round-trips through PHP to be reformatted, so typing stays smooth even when a handler is slow.
Toggles and choices
Section titled “Toggles and choices”Toggle::make($this->notifications)->onToggle(function (string $value): void { $this->notifications = $value === '1';});Selects, autocomplete, multi-select, date pickers, checkboxes and sliders are part of DesktopUI, which ships with the package.
Keyboard
Section titled “Keyboard”onKeyDown() and onKeyUp() exist on every element. The handler receives a KeyEvent:
use Pam\Desktop\KeyEvent;
Column::make(...$rows)->onKeyDown(function (KeyEvent $event): void { $this->selected = match ($event->key) { 'ArrowDown' => min($this->selected + 1, count($this->rows) - 1), 'ArrowUp' => max(0, $this->selected - 1), default => $this->selected, };
if ($event->key === 'Enter' && $event->primary()) { $this->submit(); }});| Property | Meaning |
|---|---|
key |
The W3C key value: Enter, Escape, ArrowLeft, a, F5… |
shift, control, alt, meta |
Modifiers. meta is Command on macOS and the Windows or Super key elsewhere. |
repeat |
The key is being held. |
primary() |
Command on macOS, Control elsewhere. |
A key that no text control consumed goes to the nearest listener at or above the focused element. Some keys are consumed by the focused native control first (Space presses a button, arrows move a caret). captureKeys hands them to the element instead:
<Column captureKeys="ArrowUp,ArrowDown,Enter,Space" on:keyDown="key">…</Column>Combinations with Control, Alt or Meta are never captured, so shortcuts keep working. For application-wide keys, register a shortcut instead.
- Tab and Shift+Tab move focus between focusable elements: every enabled pressable and every text input.
- Enter and Space activate the focused pressable.
autoFocus()focuses an input when it appears.- Style focus with
:focus,:focus-visibleand:focus-withinin a stylesheet.
Validated forms
Section titled “Validated forms”Extend Pam\Desktop\Forms\NativeForm with one public property per field and attach rules:
<?php
declare(strict_types=1);
namespace App\Forms;
use Pam\Desktop\Forms\Attributes\Email;use Pam\Desktop\Forms\Attributes\Matches;use Pam\Desktop\Forms\Attributes\MinLength;use Pam\Desktop\Forms\Attributes\Required;use Pam\Desktop\Forms\NativeForm;
final class SignUpForm extends NativeForm{ #[Required] #[Email] public string $email = '';
#[Required] #[MinLength(12)] public string $password = '';
#[Matches('password', message: 'The passwords do not match.')] public string $confirmation = '';}Use it from a screen:
final class SignUpScreen extends Component{ public SignUpForm $form;
#[State] public int $revision = 0; // bumped to render after the form changes
public function __construct() { $this->form = new SignUpForm(); }
public function change(string $field, string $value): void { $this->form->set($field, $value); $this->form->validate($field); $this->revision++; }
public function submit(): void { if (!$this->form->beginSubmit()) { $this->revision++; return; }
Http::json('POST', 'https://api.example.com/users', $this->form->values(), function (HttpResponse $response): void { $response->successful() ? $this->form->succeed() : $this->form->fail(json_decode($response->body, true)['errors'] ?? []); $this->revision++; }); }}| Method | Meaning |
|---|---|
set($field, $value), value($field), fill($values), values() |
Read and write fields. |
validate(?$only) |
Runs the rules of every field, or of one. |
errors(), error($field), firstErrorField() |
The messages. |
setServerErrors($errors) |
Merges errors returned by an API, keyed by field. |
beginSubmit(), succeed(), fail($errors), status() |
The submission cycle: FormStatus::Idle, Editing, Validating, Submitting, Success, Failure. |
isDirty(?$field), isTouched($field), resetInteraction() |
Interaction tracking. |
saveDraft($key), restoreDraft($key), forgetDraft($key) |
Keep an unfinished form across launches. |
The rules are Required, Email, MinLength, MaxLength and Matches. Each accepts a custom message; the default messages are localized (see Localization). validateWith(Closure) adds a rule of your own.
Next steps
Section titled “Next steps”- Windows, Menus & Tray for shortcuts and file dialogs.
- Accessibility for labels and keyboard navigation.