Forms <hw:form>
View examples

Form

Form wrapper that composes optional Stimulus behaviors via boolean props. Renders a <form> element with a default method="post".

Quick example

BLADE
<hw:form :action="route('items.index')" method="get" auto-submit clean-query-params>
    <hw:input type="search" name="q" placeholder="Search..." auto-submit />
    <hw:input type="hidden" name="category" value="books" />
    <button type="submit">Search</button>
</hw:form>

Props

Prop Type Default Description
id string|object '' Form identity and field id scope. Explicit strings pass through; objects resolve through dom_id(); omitted resolves to a deterministic hw-form-<scope>-<n> id
auto-submit bool false Adds the auto-submit controller for fields that opt into automatic submission
auto-submit-delay int|string|null null Global debounce delay for auto-submit#debouncedSubmit; the controller default is 300 ms when omitted
unsaved-changes bool false Warns before navigating away with unsaved changes
error-scroll bool false Scrolls to the first validation error after form submission
error-focus bool false Recovers lost focus on the first eligible invalid control after a validation render
clean-query-params bool false Strips empty fields from GET query strings before submission
conditional-fields bool false Adds the conditional-fields controller for nested <hw:conditional-field> dependents
track-frame-src bool false Includes a hidden _turbo_frame_src input for correct redirect resolution inside Turbo Frames
frame string|object|false|null null Sets data-turbo-frame on the form so Turbo submits into that frame; objects use dom_id() and false/blank values omit it
enctype string|null null HTML enctype attribute. Set to "multipart/form-data" for file uploads. Default null omits the attribute (browser uses application/x-www-form-urlencoded)
state mixed null Initial form state inherited by conditional fields for server-rendered visibility

Any other HTML attribute (action, method, class, data-*, aria-*) passes through to the <form> element. Method defaults to post unless overridden.

The component automatically includes @csrf for all non-GET methods and @method for PUT, PATCH, and DELETE forms.

Field id scope

The form id doubles as the scope for ids derived from field names. <hw:field name="title"> inside this form emits hw-form-page-1-title — or {form-id}-title with an explicit id — so the same name stays unique across forms in one document. Duplicate names within one form receive numeric suffixes (...-title-2). Inside a single <hw:field>, labels, aria-describedby and error nodes stay paired with their control; loose label/control/error components with a repeated name in one form get unique ids but should prefer a <hw:field> wrapper or explicit ids. Fields rendered outside an <hw:form> keep their plain name-derived ids.

The automatic hw-form-<scope>-<n> id is positional, and the scope changes between a full-page render (page) and a standalone frame render (frame-<id>). Give the form an explicit or model id when it can be refreshed alone by a Turbo Frame or Stream, or when it lives in a reorderable collection — otherwise every field id moves along with it and Turbo morph persistence degrades.

Stable ids preserve DOM identity; they do not prevent Turbo from applying server-rendered field values. Preserving an unsaved draft across a refresh requires a separate application policy.

Controllers

Controller props activate Stimulus controllers on data-controller. Multiple props compose automatically:

BLADE
{{-- Renders: data-controller="auto-submit unsaved-changes" --}}
<hw:form auto-submit unsaved-changes>
    ...
</hw:form>

auto-submit

Submits the form automatically in response to field events. Hotwire form components with an auto-submit prop wire the right event for their control type:

BLADE
<hw:form action="/search" method="get" auto-submit auto-submit-delay="300">
    <hw:input type="search" name="q" auto-submit auto-submit-delay="600" />
    <hw:slider name="max_price" :max="1000" aria-label="Maximum price" auto-submit />
    <hw:select name="category" :options="$categories" auto-submit />
</hw:form>

Text fields and sliders default to debounced submit. Discrete controls like select, checkbox, switch and toggle default to immediate submit. Use auto-submit="debounced" to force debounce on a discrete control:

BLADE
<hw:select name="category" :options="$categories" auto-submit="debounced" auto-submit-delay="500" />

For custom markup, wire actions manually with input->auto-submit#debouncedSubmit or change->auto-submit#submit.

See auto-submit controller.

Turbo Frame target

Use frame when a form should submit into a Turbo Frame. This is equivalent to writing data-turbo-frame, but keeps the common Hotwire form path declarative:

BLADE
<hw:form method="get" action="/tasks" frame="results" auto-submit>
    <hw:toggle name="status" value="done" :pressed="request('status') === 'done'" auto-submit>
        Done
    </hw:toggle>
</hw:form>

<turbo-frame id="results">
    ...
</turbo-frame>

If both frame and data-turbo-frame are present, the explicit native attribute wins. Bind :data-turbo-frame="false" to suppress a frame prop. null, false, empty strings, and whitespace-only strings omit the destination; objects resolve through dom_id().

unsaved-changes

Warns with a browser confirmation dialog when attempting to navigate away with unsaved form changes. Integrates with Turbo Drive.

BLADE
<hw:form :action="route('posts.update', $post)" method="put" unsaved-changes>
    <hw:input name="title" :value="$post->title" />
    <button type="submit">Save</button>
</hw:form>

See unsaved-changes controller.

clean-query-params

Removes empty parameters from the query string before submitting a GET form, avoiding polluted URLs like ?q=&category=.

BLADE
<hw:form action="/items" method="get" clean-query-params>
    <input type="search" name="q" />
    <select name="category">
        <option value="">All</option>
        ...
    </select>
    <button type="submit">Filter</button>
</hw:form>

See clean-query-params controller.

conditional-fields

Show or hide dependent fields as users edit the form. Add conditional-fields to the form, then declare each dependent with <hw:conditional-field>:

BLADE
<hw:form conditional-fields :state="['type' => $selectedType]">
    <hw:field name="type" label="Type">
        <hw:select :options="$types" :selected="$selectedType" />
    </hw:field>

    <hw:conditional-field when="type=link">
        <hw:field name="url" label="URL">
            <hw:input type="url" />
        </hw:field>
    </hw:conditional-field>
</hw:form>

state is the server-side fallback used for the first render before JavaScript connects. It can be an Eloquent model, array, or object readable by data_get(). On validation retries, Laravel's old() input still wins.

See conditional-field and conditional-fields controller.

error-focus

Opt into keyboard focus recovery when a validation response replaces the form, especially inside a Turbo Frame or overlay. The response's first visible, enabled, focusable [aria-invalid="true"] control receives focus so the user can correct it immediately:

BLADE
<hw:form :action="route('customers.store')" method="post" track-frame-src error-focus>
    <hw:field name="name" label="Name" required>
        <hw:input />
    </hw:field>
    <hw:field name="email" label="Email" required>
        <hw:input type="email" />
    </hw:field>
    <hw:button type="submit">Save</hw:button>
</hw:form>

This mounts autofocus with its first-invalid strategy. With no eligible errors it does nothing. It only recovers focus from the document body or the form's own dialog container; a focused field, button or another form/overlay keeps its focus. This does not change an overlay's initial-focus policy.

Hidden, inert, disabled and negative-tabindex controls are skipped. Composite controls such as Multi Select expose aria-invalid="true" on their interactive trigger, not on the hidden submission control. Rich Text forwards it to the Tiptap editor. If the first invalid Rich Text is still mounting, focus waits for that editor instead of jumping to a later error; focus chosen by the user while it loads is preserved. Custom widgets must likewise mark an eligible interactive element. Controls explicitly associated with another form are ignored.

This is not a focus policy for in-place Turbo Drive morphs: when a same-page validation redirect preserves the form and its controller, no new mount or Frame load occurs, so error-focus does not start another attempt. An input can retain focus when submitting with Enter; clicking Submit can leave focus on the body because Turbo temporarily disables that button.

Focus uses preventScroll; add error-scroll separately when the error should also be scrolled into view. While error-focus is enabled, data-autofocus-* attributes on the form are protected. For other autofocus strategies, leave the prop off and compose the standalone controller instead.

error-scroll

Scrolls to the first validation error after a form submission fails. Listens to turbo:frame-render (inside a Turbo Frame) or turbo:render (full-page morphs) and finds the first [aria-invalid] element to scroll it into view. Frame events are ignored unless the rendered frame owns, contains, or is contained by the mounted form; a page-level form wrapper still observes descendant frames. Works automatically with <hw:field> and <hw:input>, which set aria-invalid on validation errors.

BLADE
<hw:form :action="route('posts.store')" method="post" error-scroll>
    <hw:field name="title" label="Title" required>
        <hw:input />
    </hw:field>
    <button type="submit">Save</button>
</hw:form>

The controller also supports customizing the selector, scroll behavior, and block position via data-error-scroll-*-value attributes. See error-scroll controller.

CSRF and method spoofing

The component automatically includes @csrf for all non-GET methods, and @method for PUT, PATCH, and DELETE forms. You don't need to add them manually inside the slot:

BLADE
<hw:form :action="route('posts.store')" method="post">
    <hw:input name="title" />
    <button type="submit">Save</button>
</hw:form>
{{-- @csrf is automatically included --}}

<hw:form :action="route('posts.update', $post)" method="put">
    <hw:input name="title" :value="$post->title" />
    <button type="submit">Update</button>
</hw:form>
{{-- @csrf and @method('put') are automatically included --}}

GET forms (search, filters) don't get a CSRF token or method spoofing since they don't modify the state.

Frame redirect resolution

When a form lives inside a Turbo Frame (<turbo-frame>), validation failures can redirect to the wrong URL. The visible host page, the URL that rendered the frame, and the mutation endpoint are often different. The track-frame-src prop solves this by including a hidden _turbo_frame_src input with the URL of the request that rendered the form:

BLADE
<turbo-frame id="content" src="/posts/create">
    <hw:form :action="route('posts.store')" method="post" track-frame-src>
        <hw:input name="title" />
        <button type="submit">Save</button>
    </hw:form>
</turbo-frame>

For example, a board visible at /tasks can open a modal from /tasks/create and submit to POST /tasks. Validation must redirect to /tasks/create, not the board. TurboFormRequest validates the hidden source and redirects there so the response contains the matching frame and its errors.

For client-side fallback when the hidden input is unavailable, use the standalone turbo--frame-src controller. It prefers the nearest frame source, falls back to the document URL only when no frame ancestor has a source, scopes itself to descendant form requests, and never overwrites an explicit header. See frame-src controller.

Controller integrations

hotwire:check detects the enabled auto-submit, unsaved-changes, error-scroll, autofocus (via error-focus), clean-query-params, and conditional-fields integrations. These controllers auto-load from the package; publish one only when you want to customize its source.

Styling hooks

The component exposes stable data-slot hooks for preset and application CSS:

  • data-slot="form"