v0.0.2
Demos (demo panel, opens in a new tab)

Form guard

A form that asks for confirmation before the user leaves the page with unsaved changes.

zb:form · zb:form-guard-dialog zubazar/ui-pro

PRO

This component is in Zubazar UI Pro; Zubazar UI Pro brings Zubazar UI with it. Installing Zubazar UI Pro →

Basic usage

Basic usage: view
BLADE

Turn on the form's protection with dirty-check.

blade
<zb:form dirty-check class="space-y-4"
    x-data="{ changed: false }"
    x-on:input="setTimeout(() => changed = ZubazarUI.pro.formGuard.dirty)"
    x-on:change="setTimeout(() => changed = ZubazarUI.pro.formGuard.dirty)">
    <zb:input name="customer_name" label="Customer name" value="Ayşe Yılmaz" />
    <zb:checkbox name="active" label="Active customer" checked />
    <p class="text-sm text-muted" x-text="changed ? 'There are unsaved changes' : 'No changes'"></p>
</zb:form>
PropValues
dirty-checktrue | false

Livewire form

BLADE

In a Livewire form, put attributes such as wire:submit straight on zb:form.

blade
<zb:form wire:submit="save" dirty-check class="space-y-4">
    <zb:input wire:model="customer_name" label="Customer name" />
    <zb:select wire:model="district_id" label="District" :options="$districts" />
    <zb:button type="submit">Save</zb:button>
</zb:form>

After saving

PHP

When saving finishes or another record is loaded into the form, dispatch the form-saved event; the current values become the new starting point.

php
public function save(): void
{
    $this->validate();

    $this->customer->update($this->only(['customer_name', 'district_id']));

    $this->dispatch('form-saved');
}
PropValues
form-savedEvent; marks all guarded forms as clean
zb:modal:closedEvent; dispatched when zb:modal closes (the old name modal-closed is also dispatched), resets the forms inside it

Action bar

BLADE

zb:form-actions (PRO) adds Save, Cancel, Previous, Next, Add and Delete buttons to the form.

blade
<zb:form wire:submit="save" dirty-check class="space-y-6">
    <zb:input wire:model="name" label="Product name" required />
    <zb:combobox wire:model="tags" label="Tags" multiple :options="$tagOptions" />

    <zb:form-actions sticky cancel="revert"
        :record-exists="$productId !== null"
        :has-previous="$previousId !== null"
        :has-next="$nextId !== null"
        :can-delete="$canDelete">
        <p class="text-xs text-muted">{{ $name }}</p>
    </zb:form-actions>
</zb:form>
PropValues
record-exists · has-previous · has-next · can-deleteThe record's state; Previous, Next, Add and Delete are enabled from them
stickyBar that stays at the bottom of the screen
canceltrue: last clean values · "revert": Livewire method
slotStatus line at the start of the bar

Confirmation dialog

Confirmation dialog: view
BLADE

With changes, trying to go to another page opens zb:form-guard-dialog; in this example a button opens it.

blade
<zb:button variant="white" x-data x-on:click="$dispatch('form-guard-confirm', { url: null })">Show the dialog</zb:button>

<zb:form-guard-dialog />

About

The guard compares the current value of each field with its value when the page was opened; if you set a field back to its old value, the form counts as clean again. With changes, zb:form-guard-dialog opens on wire:navigate transitions and on the back/forward buttons; when the page is reloaded or the tab is closed, the browser's own warning appears.

Props

The values the component takes. What are props and slots?

PropTypeDefaultDescription
form: dirty-checkbooleanfalseTurns on unsaved-changes protection (data-dirty-check)
form: record-exists · has-previous · has-next · can-delete—nullPRO: the record's state for the action bar inside; the same as the bar's props of the same name
form-actions: record-exists · has-previous · has-next · can-delete—nullPRO: the record's state; read on every render, Previous, Next, Add and Delete are enabled from them. null: not given
form-actions: stickybooleanfalsePRO: the bar stays at the bottom of the screen (without shifting the layout)
form-actions: cancelbooleantruePRO: Cancel returns to the last clean values; when a method name is given, that Livewire method is called
form-actions: variant—buttonsPRO: pills; Previous, Next, Add and Delete become pill buttons
form-actions: when-dirty—disablePRO: confirm; with changes the buttons stay on and ask when clicked
form-actions: delete-dialogbooleantruePRO: delete is asked in the kit's dialog; false uses the browser box (wire:confirm)
form-actions: dirty—nullPRO: the server's word on unsaved changes
form-guard-dialog— — Takes no props; added to the layout once (already included in the ready-made layouts)

Slots: form: default (form fields)

Advanced

1 example

What counts

BLADE

The form's fields are compared; a field or container carrying data-dirty-ignore is not counted.

blade
<zb:form wire:submit="save" dirty-check>
    <zb:tag-input wire:model="keywords" label="Keywords" />
    <input type="search" placeholder="Search the list" data-dirty-ignore>
</zb:form>
PropValues
data-dirty-ignoreField or container; its changes are not counted

Developer notes

Events, hooks and details
  • ZubazarUI.pro.formGuard.dirty tells whether the form has changed right now; the old name window.formDirty gives the same value.
  • Guarded forms added later by a Livewire update are tracked automatically too.
  • A form with zb:form-actions inside doesn't need form-saved: the bar takes the new starting values from the request's result. When zb:modal closes, it resets only the forms inside it.
  • The action bar binds itself to the form it is in; no id, extra attribute or event is needed. The record's state props are read again on every render and can be given to zb:form too.
  • Save is enabled only when a field changes; when its request comes back without validation errors, the record counts as saved. Previous, Next, Add and Delete are enabled only on a saved, clean record.
  • Cancel restores the form to its last clean values (including wire:model values, select boxes and tag inputs); with cancel="revert", that Livewire method is called and the values it returns become the new clean state.
  • sticky keeps the bar at the bottom of the screen; since it keeps its space at the end of the form, nothing shifts when it sticks.
  • To target the form with events (such as ui:form-saved), give it an id; the event list is in the Form modal guide.
  • zb:combobox, zb:tag-input, zb:rating and zb:input-otp keep their value in hidden fields named after the property when bound with wire:model and fire change when it changes, so the guard counts them. Their search and typing boxes and code boxes are not counted.
  • The ready-made layouts (zb:layout.sidebar, zb:layout.horizontal) include the dialog; in your own layout, add zb:form-guard-dialog once.
  • In the dialog, "Continue" completes the transition and the address bar shows the new page. On pages without Livewire, the back and forward buttons show the browser's own warning.
Zubazar UI · ZubazarDesigned for people.