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
BLADE
The error is looked up by the field's name (or its wire:model); calling validate() or $request->validate() is enough.
<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>
| Prop | Values |
|---|---|
name / wire:model | Field name the error is looked up by |
Hint and error
BLADE
With an error the hint is hidden and the message takes its place; the hint comes back when the error is fixed.
<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>
| Prop | Values |
|---|---|
hint | Shown when there is no error |
Errors in lists
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.
<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>
| Prop | Values |
|---|---|
zb:radio-group · zb:checkbox-group | Shows the list's message once |
With Livewire
PHP
Keep the rules in rules(); to validate as a field changes, call validateOnly() in updated().
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
- 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 thenameandname.*errors. - Use
wire:model.live.blurwithvalidateOnly(); in Livewire 4 a plain.blursends no request, so the server never sees the value.