In laravel 11 / livewire 3.5 app Icreated a custom rule with command :
php artisan make:rule LocationIsValidRule
and having a file with declaration :
<?php
namespace App\Rules;
use App\Library\Services\Interfaces\GetGeoLocationInterface;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class LocationIsValidRule implements ValidationRule
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
// geo location with spatie package would be run if location string is valid.
...
I use it in livewire component :
class MyComponent extends Component
{
...
protected function rules()
{
return [
'title' => [
'required',
...
],
'location' => [
'required',
'string',
'min:3',
'max:100',
[new LocationIsValidRule()],
],
];
}
But on validation I got error :
trim(): Argument #1 ($string) must be of type string,
App\Rules\LocationIsValidRule given
and pointing to:
$this->validate();
line of store method.
I read docs at https://livewire.laravel.com/docs/validation#using-custom-validators , but looks it is somewhat different case>
How have I to call custom validation in livewire app ?
UPDATED :
Not sure if command
php artisan make:rule
valid in this case, Which format must have custom validation rule in livewire ?
You should remove the square brackets around the custom rule
'location' => [
'required',
'string',
'min:3',
'max:100',
new LocationIsValidRule(),
],
See https://laravel.com/docs/11.x/validation#custom-validation-rules