Live Wire how to trigger emit when validate fails in livewire

 

Basicly, You don't have to check for if ($validator->fails()) livewire does that for you.

If validation fails, a standard ValidationException is thrown (and caught by Livewire), and the standard $errors object is available inside the component's view.

 In order to hook into the validation in "real-time" with Livewire, you need to catch that exception. To get the real-time validation with @error('fieldName'), you should also re-throw the exception once you are done.

 

in my case i need to send emit ('error message') when  $validator->fails()

and now this is the code

 

// Proses Validasi///////////////////////////////////////////
try {
$this->validate($rules, $messages);
} catch (\Illuminate\Validation\ValidationException $e) {

// dd($validator->fails());
$this->emit('toastr-error', "Lakukan Pengecekan kembali Input Data Pasien.");
$this->validate($rules, $messages);
}