mes's avatar
Level 5

Tell if model is valid without filling error bag in livewire

Hi!

I'm experimenting with Livewire (so far loving it!) and was wondering if there is any way in my Livewire component to tell if my Eloquent model is valid, but without calling $this->validate() as I don't want the errors to be displayed. I just need to know if the model is valid or not. Probably this is more a generic Laravel question than a Livewire one, I'm pretty new to both. Thanks!

0 likes
2 replies
Nakov's avatar
Nakov
Best Answer
Level 73

You can manually create a Validator: https://laravel.com/docs/9.x/validation#manually-creating-validators

and don't use $this->validate() as that one will always fill in the error bag and display the errors as long as you are displaying them..

Just an example of manually validating because there is no $request that you can use

public $title;

public function save()
{
	$validator = Validator::make(['title' => $this->title], ['title' => 'required']);

	if ($validator->fails())
	{
		// handle it here 
	}
}

Please or to participate in this conversation.