I want the errors show of the controller not the View
public $name;
public $email;
protected $rules = [
'name' => 'required|min:6',
'email' => 'required|email',
];
public function submit()
{
$this->validate();
Contact::create([
'name' => $this->name,
'email' => $this->email,
]);
}
In Laravel, i can bring the errors array in this way, but how can I bring them back from Livewire?
if ($validator->fails())
{
return dd($validator->messages());
}
Using $this->validate()
will throw an exception when there are validation-errors. This exception can be caught in a try
/catch
, and if its not caught, Laravel will catch it with its global exception-handler (which will enable the @error
directive in blade, or the $errors
variable in your view.
You can intercept this by catching it yourself and handling the logic you want to apply. Its also recommended that you re-throw the exception after that, so that execution of the method stops, and that you get the default Laravel behavior in your view.
public function submit()
{
try {
$this->validate();
} catch (\Illuminate\Validation\ValidationException $e) {
// Use $e->errors() to find the validationerrors
// Add your custom logic here.
// Re-throw the exception once done
throw $e;
}
Contact::create([
'name' => $this->name,
'email' => $this->email,
]);
}