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
This component is in Zubazar UI Pro; Zubazar UI Pro brings Zubazar UI with it. Installing Zubazar UI Pro →
Basic usage
Turn on the form's protection with dirty-check.
<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>
| Prop | Values |
|---|---|
dirty-check | true | false |
Livewire form
In a Livewire form, put attributes such as wire:submit straight on zb:form.
<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
When saving finishes or another record is loaded into the form, dispatch the form-saved event; the current values become the new starting point.
public function save(): void
{
$this->validate();
$this->customer->update($this->only(['customer_name', 'district_id']));
$this->dispatch('form-saved');
}
| Prop | Values |
|---|---|
form-saved | Event; marks all guarded forms as clean |
zb:modal:closed | Event; dispatched when zb:modal closes (the old name modal-closed is also dispatched), resets the forms inside it |
Action bar
zb:form-actions (PRO) adds Save, Cancel, Previous, Next, Add and Delete buttons to the form.
<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>
| Prop | Values |
|---|---|
record-exists · has-previous · has-next · can-delete | The record's state; Previous, Next, Add and Delete are enabled from them |
sticky | Bar that stays at the bottom of the screen |
cancel | true: last clean values · "revert": Livewire method |
slot | Status line at the start of the bar |
Confirmation dialog
With changes, trying to go to another page opens zb:form-guard-dialog; in this example a button opens it.
<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?
| Prop | Type | Default | Description |
|---|---|---|---|
form: dirty-check | boolean | false | Turns on unsaved-changes protection (data-dirty-check) |
form: record-exists · has-previous · has-next · can-delete | — | null | PRO: 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 | — | null | PRO: the record's state; read on every render, Previous, Next, Add and Delete are enabled from them. null: not given |
form-actions: sticky | boolean | false | PRO: the bar stays at the bottom of the screen (without shifting the layout) |
form-actions: cancel | boolean | true | PRO: Cancel returns to the last clean values; when a method name is given, that Livewire method is called |
form-actions: variant | — | buttons | PRO: pills; Previous, Next, Add and Delete become pill buttons |
form-actions: when-dirty | — | disable | PRO: confirm; with changes the buttons stay on and ask when clicked |
form-actions: delete-dialog | boolean | true | PRO: delete is asked in the kit's dialog; false uses the browser box (wire:confirm) |
form-actions: dirty | — | null | PRO: 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
What counts
The form's fields are compared; a field or container carrying data-dirty-ignore is not counted.
<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>
| Prop | Values |
|---|---|
data-dirty-ignore | Field or container; its changes are not counted |
Developer notes
ZubazarUI.pro.formGuard.dirtytells whether the form has changed right now; the old namewindow.formDirtygives the same value.- Guarded forms added later by a Livewire update are tracked automatically too.
- A form with
zb:form-actionsinside doesn't needform-saved: the bar takes the new starting values from the request's result. Whenzb:modalcloses, 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 tozb:formtoo. - 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:modelvalues, select boxes and tag inputs); withcancel="revert", that Livewire method is called and the values it returns become the new clean state. stickykeeps 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 anid; the event list is in the Form modal guide. zb:combobox,zb:tag-input,zb:ratingandzb:input-otpkeep their value in hidden fields named after the property when bound withwire:modeland firechangewhen 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, addzb:form-guard-dialogonce. - 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.