fix(users): pass mobile rule as a nested closure Filament can evaluate

Filament evaluates each entry of ->rules() as its own closure and tries
to inject dependencies, so a bare Laravel validation closure blew up with
"[$attribute] was unresolvable" when opening/saving the user form. Wrap it
so the outer closure returns the Laravel rule.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-09 00:37:37 +03:30
parent ac11fbeaa6
commit f31f87aaf6
2 changed files with 55 additions and 5 deletions

View File

@@ -28,11 +28,13 @@ class UserForm
->tel() ->tel()
->helperText('مثال درست: 09123456789') ->helperText('مثال درست: 09123456789')
->dehydrateStateUsing(fn (?string $state): ?string => Mobile::normalize($state)) ->dehydrateStateUsing(fn (?string $state): ?string => Mobile::normalize($state))
->rules([static function (string $attribute, mixed $value, \Closure $fail): void { ->rules([
if (filled($value) && ! preg_match(Mobile::REGEX, (string) Mobile::normalize((string) $value))) { fn (): \Closure => function (string $attribute, mixed $value, \Closure $fail): void {
$fail('فرمت شماره موبایل صحیح نیست. مثال درست: 09123456789'); if (filled($value) && ! preg_match(Mobile::REGEX, (string) Mobile::normalize((string) $value))) {
} $fail('فرمت شماره موبایل صحیح نیست. مثال درست: 09123456789');
}]) }
},
])
->unique(ignoreRecord: true) ->unique(ignoreRecord: true)
->maxLength(20), ->maxLength(20),
TextInput::make('password') TextInput::make('password')

View File

@@ -0,0 +1,48 @@
<?php
use App\Filament\Resources\Users\Pages\CreateUser;
use App\Models\Role;
use App\Models\User;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use function Pest\Laravel\actingAs;
uses(RefreshDatabase::class);
beforeEach(function (): void {
Filament::setCurrentPanel(Filament::getDefaultPanel());
$admin = User::factory()->create();
$role = Role::query()->firstOrCreate(['slug' => 'admin'], ['name' => 'مدیر']);
$admin->roles()->sync([$role->id]);
actingAs($admin);
});
it('opens the create user form without the unresolvable-closure error', function (): void {
Livewire\Livewire::test(CreateUser::class)
->assertOk();
});
it('accepts a +98 mobile and normalizes it, rejects a malformed one', function (): void {
Livewire\Livewire::test(CreateUser::class)
->fillForm([
'name' => 'کاربر تست',
'email' => 'ok@example.com',
'mobile' => '+98 912 345 6789',
'password' => 'password12',
])
->call('create')
->assertHasNoFormErrors();
expect(User::query()->where('email', 'ok@example.com')->value('mobile'))->toBe('09123456789');
Livewire\Livewire::test(CreateUser::class)
->fillForm([
'name' => 'کاربر بد',
'email' => 'bad@example.com',
'mobile' => '123',
'password' => 'password12',
])
->call('create')
->assertHasFormErrors(['mobile']);
});