Courses

Practical Laravel Livewire 2 from Scratch

Communication Between Components: Emit Events

Previous: Even Shorter Code with Magic Actions: $set and $toggle
avatar

Can I use events to get the validation error from the child components to the parent? I have a huge form split up in livewire components for each Model using a multistep form. But the user may type in everything or nothing in each child form (save your work for later). Validation errors should not be seen on the child component, but at the final step when the user tried to send the application. Should I do this with events or is there any other way?

avatar

I haven't tried this specific scenario, and without playing around and debugging it's hard to answer 100% correct, but I would probably save those validation errors in a session variable maybe and then show them at the end.

avatar
You can use Markdown
avatar

I'd like to share with guys a nice thing I just tried.

We can use the window.livewire.on() method to execute a js code when an event is emitted and show, for example, a sweet alert. Something like this (under the @livewireScripts):

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<script>
window.livewire.on('profileUpdated', function() {
		Swal.fire({
				title: 'Profile Updated',
				text: 'You have updated your profile',
				icon: 'success',
				confirmButtonText: 'Cool'
		})
});
</script>

You can see the code on my commit: https://github.com/antonioanerao/laravel-livewire/commit/d045f0a5d3ddf9d7069a0d8bf0829b627569401a

👍 2
avatar
You can use Markdown
avatar
You can use Markdown