Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #588
    dan
    Participant

    How can I handle form validation in a Livewire component?

    #591

    Livewire provides built-in validation methods. You can use validate() method to handle form validation in your component.

    // Livewire component class
    class MyComponent extends Component
    {
    public $name;

    public function submit()
    {
    $this->validate([
    ‘name’ => ‘required|min:3’,
    ]);

    // Handle form submission
    }

    public function render()
    {
    return view(‘livewire.my-component’);
    }
    }

    <!– Blade view for MyComponent –>
    <div>
    <form wire:submit.prevent=”submit”>
    <input type=”text” wire:model=”name”>
    @error(‘name’) <span class=”error”>{{ $message }}</span> @enderror
    <button type=”submit”>Submit</button>
    </form>
    </div>

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.