Tech Verse Logo
Enable dark mode
Simplifying Form Validation with Laravel Livewire's #[Validate] Attribute

Simplifying Form Validation with Laravel Livewire's #[Validate] Attribute

Md. Mostafijur RahmanMMd. Mostafijur Rahman

Md. Mostafijur Rahman

β€’1 min read

Livewire v3’s #[Validate] attribute is one of those changes that looks small but really shifts how we structure components. Instead of juggling a big $rules array at the bottom of your class (where you constantly scroll back and forth to see which property it applies to), the rules now live with the property itself β€” much closer to how we think about form data in real-world components.

Here are some practical highlights worth noting:

πŸ”‘ Key Benefits

  1. Co-located rules β†’ You don’t have to mentally map rules to properties; the contract is right there.

  2. Cleaner components β†’ No more giant $rules arrays cluttering your class.

  3. Custom messages inline β†’ Error messages live next to the property, easier to maintain.

  4. Conditional validation β†’ Still supports required_if, sometimes, and even complex nested rules.

  5. Array support β†’ Perfect for collections like tags, checklists, or dynamic form fields.

πŸ›  Usage Patterns

Inline validation

#[Validate('required|string|min:3')]
public $name = '';

With custom message

#[Validate('required|min:2', message: 'Name must be at least 2 characters')]
public $name = '';

With multiple rules & messages

#[Validate(
    rules: [
        'tags' => 'required|array|min:1',
        'tags.*' => 'string|max:50'
    ],
    message: [
        'tags.required' => 'Please add at least one tag',
        'tags.*.max' => 'Tags may not exceed 50 characters'
    ]
)]
public $tags = [];

πŸš€ Real-World Example: User Profile Form

class ProfileForm extends Component
{
    #[Validate('required|string|min:3')]
    public $name = '';

    #[Validate('required|email|unique:users,email')]
    public $email = '';

    #[Validate('nullable|image|max:2048')]
    public $avatar;

    public function save()
    {
        $data = $this->validate();

        auth()->user()->update($data);

        session()->flash('success', 'Profile updated successfully!');
    }

    public function render()
    {
        return view('livewire.profile-form');
    }
}

βš–οΈ When to Still Use $rules

  • When you need to dynamically generate rules (e.g., based on DB values or permissions).

  • If you prefer having all rules centralized in one place (some teams like the overview).

  • For very complex multi-field validations that may feel verbose inline.

Md. Mostafijur RahmanMMd. Mostafijur Rahman

WRITTEN BY

Md. Mostafijur Rahman

    Latest Posts

    View All

    React Context: When to Reach for a Store

    React Context: When to Reach for a Store

    Custom React Hooks: Abstraction vs Indirection

    Custom React Hooks: Abstraction vs Indirection

    Stop Wasting React Performance on memo and useMemo

    Stop Wasting React Performance on memo and useMemo

    Next.js Partial Prerendering: Static Shells, Dynamic Holes

    Next.js Partial Prerendering: Static Shells, Dynamic Holes

    Stop Re-rendering: When to Drop useEffect in React 19

    Stop Re-rendering: When to Drop useEffect in React 19

    Fixing Next.js Hydration Mismatch Errors in React 19

    Fixing Next.js Hydration Mismatch Errors in React 19

    Generating Open Graph Images at the Edge in Next.js 16

    Generating Open Graph Images at the Edge in Next.js 16

    Fix Next.js Third Party Script Performance

    Fix Next.js Third Party Script Performance

    Next.js 16 Testing Strategy: Unit, Component, E2E

    Next.js 16 Testing Strategy: Unit, Component, E2E

    Shrinking Next.js Bundle Size: Analyzer & Barrel Fixes

    Shrinking Next.js Bundle Size: Analyzer & Barrel Fixes