API Index — Namespaces, Packages, Reports, Indices
HtmxBinder
extends FormBinder
in package
The first-class binder: makes components interactive with htmx (2.x) attributes on top of the plain-form markup, so pages still work without JS.
- Actions and live/blur inputs post to the component endpoint, include
every control inside the component (
hx-include="closest [data-zc]"), and swap the re-rendered component in place. - An action always goes out: it replaces (aborts) a live update still in flight on the same component — the action request carries every field anyway. It doesn't queue behind it, because htmx drops a queued request whose element was replaced by the swap. The clicked button is disabled until its response arrives, so a double-click can't re-send a stale snapshot.
- Live updates queue (
queue last) behind one another; the input that triggered them is preserved across the swap, so the queued request still has its element. - The control that triggered a live update gets
hx-preserve, so htmx keeps the element the user is typing in (value, focus and cursor) while the rest of the component is replaced. - Validation errors come back as 200 (htmx doesn't swap 4xx by default);
a snapshot from an older deploy answers
HX-Refresh: true.
htmx: 2 (default) or htmx: 4 picks the attribute dialect for pages.
A component request tells the binder which htmx sent it (htmx 4 adds
HX-Request-Type), so its response always uses that version's dialect,
whatever the setting; the setting only decides the first page render.
The dialects:
- htmx 4 always swaps with its built-in
outerMorph. Morphing keeps the DOM nodes, and htmx 4 never overwrites the value of the focused input, so the field being typed in survives withouthx-preserve(which in htmx 4 only works when the existing element already carries it). Kept nodes also keep queued requests alive. The clicked button is disabled withhx-disable(which means "ignore this element" in htmx 2). - htmx 2 swaps
outerHTMLand marks the triggering fieldhx-preserve;morph: trueuses the idiomorph extension instead (morph:outerHTML, needs the extension script on the page).
Every attribute is set on the element that needs it, never inherited, so
htmx 4's explicit inheritance changes nothing. Refused requests answer
HX-Reswap: none, so htmx 4 (which swaps 4xx/5xx by default) leaves the
component in place.
Table of Contents
Constants
- TARGET : mixed = 'closest [data-zc]'
Properties
Methods
- __construct() : mixed
- actionAttributes() : array<string, string|bool|null>
- Attributes for an element that triggers an action.
- isAsync() : bool
- Whether the request came from this binder's client script (rather than a plain form post that expects a redirect back to the page).
- modelAttributes() : array<string, string|bool|null>
- Attributes for a form control bound to a
#[Model]property. - refusalHeaders() : array<string, string>
- Extra headers for a refused request (400/403/404), e.g. telling the client not to swap the error text into the page.
-
response()
: array{status: int, headers: array
} - Status and headers for an endpoint outcome.
- rootAttributes() : array<string, string|bool|null>
- Extra attributes for the component's outer element.
- triggerField() : string|null
- The name of the form control that triggered the request, if the client reports it.
- dialect() : int
- The htmx version to write attributes for: the one that sent the current request, else the configured one (a page render has no htmx request).
- swap() : string
Constants
TARGET
private
mixed
TARGET
= 'closest [data-zc]'
Properties
$htmx read-only
private
int
$htmx
= 2
$morph read-only
private
bool
$morph
= false
Methods
__construct()
public
__construct([bool $morph = false ][, int $htmx = 2 ]) : mixed
Parameters
- $morph : bool = false
- $htmx : int = 2
actionAttributes()
Attributes for an element that triggers an action.
public
actionAttributes(Component $component, string $url, string $argsJson) : array<string, string|bool|null>
Parameters
- $component : Component
- $url : string
- $argsJson : string
-
the action arguments, JSON-encoded
Return values
array<string, string|bool|null>isAsync()
Whether the request came from this binder's client script (rather than a plain form post that expects a redirect back to the page).
public
isAsync(array<string|int, mixed> $server) : bool
Parameters
- $server : array<string|int, mixed>
-
CGI-style request variables (
HTTP_*)
Return values
boolmodelAttributes()
Attributes for a form control bound to a #[Model] property.
public
modelAttributes(Component $component, string $prop, Model $model, string $updateUrl, bool $preserve) : array<string, string|bool|null>
Parameters
- $component : Component
- $prop : string
- $model : Model
- $updateUrl : string
-
the component's
_updateendpoint - $preserve : bool
-
true when this control triggered the current update and should be left untouched by the swap
Return values
array<string, string|bool|null>refusalHeaders()
Extra headers for a refused request (400/403/404), e.g. telling the client not to swap the error text into the page.
public
refusalHeaders() : array<string, string>
Return values
array<string, string>response()
Status and headers for an endpoint outcome.
public
response(Outcome $outcome, array<string|int, mixed> $events, string|null $redirect) : array{status: int, headers: array}
Parameters
- $outcome : Outcome
- $events : array<string|int, mixed>
-
event name → detail
- $redirect : string|null
Return values
array{status: int, headers: arrayrootAttributes()
Extra attributes for the component's outer element.
public
rootAttributes(Component $component, string|null $submitUrl) : array<string, string|bool|null>
Parameters
- $component : Component
- $submitUrl : string|null
-
the URL of the component's
$submitaction, if any
Return values
array<string, string|bool|null>triggerField()
The name of the form control that triggered the request, if the client reports it.
public
triggerField(array<string|int, mixed> $server) : string|null
Parameters
- $server : array<string|int, mixed>
Return values
string|nulldialect()
The htmx version to write attributes for: the one that sent the current request, else the configured one (a page render has no htmx request).
private
dialect() : int
Return values
intswap()
private
swap() : string