Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Livewire clear modal on close

David Carr

Laravel Livewire

When you have a model that's been partially filled and a modal is closed using Alpine ie:

<button type="button" @click="on = false">Close</button>

The modal will close but anything inputted into a form inside the modal will remain.

To clear the form out when a model closes call a method on the component instead:

<button type="button" wire:click="close">Close</button>

Next, create the method in the class and either call $this->reset() to reset all properties on the class or specify the properties to be cleared.

Finally fire an event called close-modal

public function close()
{
    $this->reset(['name', 'email']);
    $this->dispatchBrowserEvent('close-modal');
}

 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.