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

Form validation

Form fields show Laravel validation errors from $errors on their own; no extra code is needed.

$errors zubazar/ui · free (MIT)

Failed submission

Failed submission: view
BLADE

The error is looked up by the field's name (or its wire:model); calling validate() or $request->validate() is enough.

blade
<form class="grid gap-4 md:grid-cols-2">
    <zb:input wire:model="name" label="Customer name" required />
    <zb:input-group wire:model="phone" label="Phone" prefix="+90" value="532 11" />
    <zb:select wire:model="group" label="Customer group" :options="['1' => 'Individual']" placeholder="Select…" />
    <zb:input-group wire:model="creditLimit" label="Credit limit" suffix="₺" value="0" />
    <div class="md:col-span-2"><zb:button type="submit">Save</zb:button></div>
</form>
PropValues
name / wire:modelField name the error is looked up by

Hint and error

Hint and error: view
BLADE

With an error the hint is hidden and the message takes its place; the hint comes back when the error is fixed.

blade
<div class="grid gap-4 md:grid-cols-2">
    <zb:input name="password_hint" type="password" label="Password" hint="At least 8 characters." />
    <zb:input name="password_error" type="password" label="Password" hint="At least 8 characters." value="1234" />
</div>
PropValues
hintShown when there is no error

Errors in lists

Errors in lists: view
BLADE

Fields that are part of a list (radios, name[] checkboxes) only get a red border; zb:radio-group or zb:checkbox-group shows the message once.

blade
<div class="space-y-6">
    <zb:radio-group label="Delivery" name="delivery" required>
        <zb:radio name="delivery" value="shipping" label="Shipping" />
        <zb:radio name="delivery" value="store" label="Store pickup" />
    </zb:radio-group>

    <zb:checkbox name="terms" label="I accept the terms of use" />
</div>
PropValues
zb:radio-group · zb:checkbox-groupShows the list's message once

With Livewire

PHP

Keep the rules in rules(); to validate as a field changes, call validateOnly() in updated().

php
protected function rules(): array
{
    return [
        'name' => ['required', 'string', 'max:120'],
        'phone' => ['required', 'digits:10'],
        'group' => ['required', 'exists:customer_groups,id'],
    ];
}

public function updated(string $field): void
{
    $this->validateOnly($field);
}

public function save(): void
{
    $data = $this->validate();
    // …
}

Developer notes

Events, hooks and details
  • Fields that show their own error: text fields, select, combobox, a single checkbox, switch, tag input, verification code, range and star rating.
  • Checkboxes, switches and option cards named name[] or bound to an array also just turn red; the group message shows the name and name.* errors.
  • Use wire:model.live.blur with validateOnly(); in Livewire 4 a plain .blur sends no request, so the server never sees the value.
Zubazar UI · ZubazarDesigned for people.