Adam Holmes
Adam Holmes
FFilament
Created by Adam Holmes on 6/17/2024 in #❓┊help
Prompt input via modal on save
Hi, I have a status field on a form which is a ToggleButton populated by an enum. I would like to add some functionality so that when I save the form, if this field has changed to a specific option then a modal should appear and the user should enter a reason (which is just a text area). I would like the reason to save into a different database table as the form I'm on because I'd like to save individual rows for each change so that I can report on all the changes later (rather than overwriting the reason each time). I'm at a real loss of how to make this happen as I'm fairly new to filament. I managed to make it overwrite the data on the same model but hiding / showing a field, but that's not what I want - I want a modal to popup on save. I also tried to add a relationship to a Group but that is still a HasOne where I would like a HasMany. Any ideas or pointers in the right direction would be much appreciated. Thanks
7 replies
FFilament
Created by Adam Holmes on 5/14/2024 in #❓┊help
Testing form with relationships
Hi, I'm attempting to test a create form for my Users page. The form has basic info on, as well as a roles dropdown which uses filament-shield. When using the UI, everything works fine. When I run the test to make sure there are no errors, the test fails with the following and I can't work out why:
FAILED Tests\Feature\UserTest > `Create Page` can save form
Component has errors: "data.roles"
Failed asserting that false is true.

at vendor/livewire/livewire/src/Features/SupportValidation/TestsValidation.php:109
105▕ {
106▕ $errors = $this->errors();
107▕
108▕ if (empty($keys)) {
109▕ PHPUnit::assertTrue($errors->isEmpty(), 'Component has errors: "'.implode('", "', $errors->keys()).'"');
110▕
111▕ return $this;
112▕ }
113▕
FAILED Tests\Feature\UserTest > `Create Page` can save form
Component has errors: "data.roles"
Failed asserting that false is true.

at vendor/livewire/livewire/src/Features/SupportValidation/TestsValidation.php:109
105▕ {
106▕ $errors = $this->errors();
107▕
108▕ if (empty($keys)) {
109▕ PHPUnit::assertTrue($errors->isEmpty(), 'Component has errors: "'.implode('", "', $errors->keys()).'"');
110▕
111▕ return $this;
112▕ }
113▕
Test
test('can save form', function () {
$user = User::factory()->make();

livewire(CreateUser::class)
->fillForm($user->toArray())
->call('create')
->assertHasNoFormErrors();
});
test('can save form', function () {
$user = User::factory()->make();

livewire(CreateUser::class)
->fillForm($user->toArray())
->call('create')
->assertHasNoFormErrors();
});
Factory
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
'status' => $this->faker->randomElement(UserStatus::class),
];
}
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
'status' => $this->faker->randomElement(UserStatus::class),
];
}
Roles dropdown in form schema
Select::make('roles')
->relationship('roles', 'name')
->getOptionLabelFromRecordUsing(function (Model $record) {
/** @var Role $record */
return UserRole::getRoleLabel($record->name);
})
->preload()
->searchable()
->required()
->dehydrated(false),
Select::make('roles')
->relationship('roles', 'name')
->getOptionLabelFromRecordUsing(function (Model $record) {
/** @var Role $record */
return UserRole::getRoleLabel($record->name);
})
->preload()
->searchable()
->required()
->dehydrated(false),
TIA, Adam
3 replies
FFilament
Created by Adam Holmes on 5/10/2024 in #❓┊help
editOptionForm with permissions
Hi, I have a select form component which uses a relationship to look at another model. I use editOptionForm to create a modal so that I can edit the selected field. This all works fine. However, I would like the edit button to only show for certain users (which I'll manage with spatie) - but I'm not sure how to make the button show / hide? Any ideas? Thanks
3 replies
FFilament
Created by Adam Holmes on 4/26/2024 in #❓┊help
Option to set default table pagination
Hi, I'm creating some tables that have plenty data in them. I have pagination enabled with the default options of [5, 10, 25, 50, 'all'] - I'd like to keep those options, but default the view to 25 rather than 10. Looking at CanPaginateRecords I can see the below method, but it seems that we either show 10 rows if it's in the options array, otherwise we just use the first item. I could override the default options and change 10 to 9 or 11, but that doesn't feel right.
public function getDefaultPaginationPageOption(): int | string | null
{
$option = $this->evaluate($this->defaultPaginationPageOption);

if ($option) {
return $option;
}

$options = $this->getPaginationPageOptions();

if (in_array(10, $options)) {
return 10;
}

return Arr::first($options);
}
public function getDefaultPaginationPageOption(): int | string | null
{
$option = $this->evaluate($this->defaultPaginationPageOption);

if ($option) {
return $option;
}

$options = $this->getPaginationPageOptions();

if (in_array(10, $options)) {
return 10;
}

return Arr::first($options);
}
Is there a way that I can get round this? Or request it as a feature in a future release? Cheers Adam
8 replies
FFilament
Created by Adam Holmes on 4/25/2024 in #❓┊help
Writing PEST tests for Filament Tables
No description
12 replies
FFilament
Created by Adam Holmes on 4/16/2024 in #❓┊help
Query string in pagination
Hi, From an edit resource I have a button that links to another list (Audits) resource with 2 parameters like so:
Actions\Action::make(name: 'Audit Logs')
->url(function() {
return route('filament.app.resources.audit-logs.index', ['resource' => ListItemResource::getModel(), 'id' => $this->record->id]);
}),
Actions\Action::make(name: 'Audit Logs')
->url(function() {
return route('filament.app.resources.audit-logs.index', ['resource' => ListItemResource::getModel(), 'id' => $this->record->id]);
}),
In my audit list resource I then filter all the audits (this is a polymorphic table and stores the model class name and the model id - note that this is using Laravel Auditing - https://laravel-auditing.com/) based on the 2 GET parameters that are passed like so:
return $table
->modifyQueryUsing(function (Builder $query) {
$query->where('auditable_type', request()->get('resource'));
$query->where('auditable_id', request()->get('id'));
})
return $table
->modifyQueryUsing(function (Builder $query) {
$query->where('auditable_type', request()->get('resource'));
$query->where('auditable_id', request()->get('id'));
})
Which all works fine on the first load. However, when I change the pagination my request object is reset to null and therefore the above query no longer works. With Laravel you can do ::paginate()->withQueryString() but I can't find anything similar with Filament. Any ideas to ensure that the request object sticks around, or a better solution to this problem that requires no sending of things in the request? Thanks 🙂
8 replies
FFilament
Created by Adam Holmes on 4/16/2024 in #❓┊help
Unable to add form actions to modal
Hi, I've got a resource with a simple form for creating / editing. I've got a table that displays all the items on the index. Each row in the table has an edit button that opens the edit page in a modal slideover. At the bottom there is a "Save changes" and "Cancel" button. I'd like to add an additional button to the bottom, but I'm unable to get it to show. I added the below to my EditItem.php in the Resource Pages directory and the button didn't show.
protected function getFormActions(): array
{
return [
...parent::getFormActions(),
Action::make('foo')->action('bar'),
];
}
protected function getFormActions(): array
{
return [
...parent::getFormActions(),
Action::make('foo')->action('bar'),
];
}
I also tried getModalActions which made no difference. If I change the page to be a separate page rather than a modal, the button does appear - but that's not what I want. I'm at a loss, so any ideas welcome. Thanks
5 replies
FFilament
Created by Adam Holmes on 4/4/2024 in #❓┊help
Logo in Sidebar
No description
8 replies