Skip to main content

Posts

Showing posts with the label livewire

Real-time Validation in livewire

 Livewire gives you the opportunity to make real-time validation of your form. let me show you how it works. Inside your components class namespace App\Http\Livewire\Tuts ; use Livewire\ Component ; class ContactForm extends Component {     public $name ;     public $email ;     public $message ;     protected $rules = [         'name' => [ 'required' ],         'email' => [ 'required' , 'email' ],         'message' => [ 'required' , 'max:10' ]     ];     public function render ()     {         return view ( 'livewire.tuts.contact-form' )-> layout ( 'layouts.app' )-> slot ( 'main' );     }     public function updated ( $propertyName )     {         $this -> validateOnly ( $propertyName );     }     public function submit (){ ...

Life cycle hooks in livewire

 LifeCycle Hooks in livewire are following 1. mount() 2. hydrate() 3. dehydrate() 4. updating() // when the public property is updating, the updating method automatically calls. 5. updated() // when the public property is updated, the updated method automatically call.

Event and listeners in livewire

 Now are gonna see events and listeners in livewire and see how they work in livewire. We can fire events  1. In the view 2. In the component 3. In javascript Now i want to emit a global event. So there are four ways to emit. 1. $this->emit() this is Global. 2. $this->emitup() child to parent. 3. $this->emitSelf() emit an event to self in the current component. 4. $this->emitTo() emit event to specific livewire component. e.g: $this->emitTo(UserDetails,'parameter'); In the View <h3>Emit Browser Event</h3> <button wire:click= " $emit ('openStore')" class = "btn btn-primary btn-md my-2 mx-2" >Emit Event</button> In javascript <!-- livewire Events --> <script>    window. addEventListener ( 'openTheShop' , event => {        Livewire. emit ( 'openStore' );    }); </script>       Most of the time we use javascript events to show alert and toast messages as well.  ...