w5m
w5m
FFilament
Created by w5m on 11/8/2024 in #❓┊help
Refresh form Select component via Section headerAction (inside Resource file)
I've been attempting and failing to refresh a Select component after calling an approve() method on a Review model, which sets a model attribute called review_status_id to ReviewStatus::Approved (an enum case) and saves the record. This is all within the ReviewResource file. If I refresh the browser, I see the updated status, but I'd like the Select component automatically display the new status as the selected option. I'm sure this is easy to achieve, but unfortunately my knowledge of Livewire is somewhat limited at the moment.
class ReviewResource extends Resource
{
protected static ?string $model = Review::class;

public static function form(Form $form): Form
{
return $form
->schema([
Section::make('Review Details')
->headerActions([
Action::make('approve')
->requiresConfirmation()
->action(function (Review $record): void {
$record->approve();
$record->refresh(); // this line has no effect
}),
])
->schema([
Select::make('review_status_id')
->label('Review status')
->options(ReviewStatus::class)
->live()
])
]);
}
}
class ReviewResource extends Resource
{
protected static ?string $model = Review::class;

public static function form(Form $form): Form
{
return $form
->schema([
Section::make('Review Details')
->headerActions([
Action::make('approve')
->requiresConfirmation()
->action(function (Review $record): void {
$record->approve();
$record->refresh(); // this line has no effect
}),
])
->schema([
Select::make('review_status_id')
->label('Review status')
->options(ReviewStatus::class)
->live()
])
]);
}
}
Any pointers would be gratefully received.
2 replies
FFilament
Created by w5m on 10/4/2024 in #❓┊help
How to get all options from table filter in table header action?
I've created a custom filter form for a table on a relation page (i.e. a class which extends ManageRelatedRecords). I have a Select component as part of the filter form and this is populated with a list of projects. Within a table header action, I'd like to retrieve the list of projects (i.e. Select options), in order to provide navigation to one of the projects in the list. Unfortunately, I'm struggling to access the Select component, so would be grateful to be pointed in the right direction.
public function table(Table $table): Table
{
return $table
->columns([
// ...
])
->filters([
Filter::make('client_project')
->form([
Select::make('client_id'),
Select::make('project_id')
])
->query(function (Builder $query, array $data) {
// ...
}),
])
->headerActions([
Tables\Actions\Action::make('next-project')
->url(function (\Livewire\Component $livewire): string {
$filters = $livewire->getTable()->getFilters();
dd($filters['client_project']);
// how do I retrieve all of the options listed in the 'project_id' Select component?
})
]);
}
public function table(Table $table): Table
{
return $table
->columns([
// ...
])
->filters([
Filter::make('client_project')
->form([
Select::make('client_id'),
Select::make('project_id')
])
->query(function (Builder $query, array $data) {
// ...
}),
])
->headerActions([
Tables\Actions\Action::make('next-project')
->url(function (\Livewire\Component $livewire): string {
$filters = $livewire->getTable()->getFilters();
dd($filters['client_project']);
// how do I retrieve all of the options listed in the 'project_id' Select component?
})
]);
}
10 replies
FFilament
Created by w5m on 8/22/2024 in #❓┊help
"data." prefix on greater than form validation message
Not sure if I'm missing something obvious here, but I'm wanting to make sure one form field is greater than 4 * value of another field. I've achieved this by passing a closure to the ->gt() method...
)->gt(function (Get $get): float {
return 4 * floatval($get('value03'));
}),
)->gt(function (Get $get): float {
return 4 * floatval($get('value03'));
}),
...but the resulting validation message is preceded by a data. prefix...
The ... field must be greater than data.128.
The ... field must be greater than data.128.
Is there a way to remove this prefix without creating my own custom rules?
1 replies
FFilament
Created by w5m on 8/5/2024 in #❓┊help
Pass form data (`$state`) to `ChartWidget` in resource Create page
I have embedded a ChartWidget in a resource Create page (of type CreateRecord) via the getFooterWidgets() method. Is there a way for the ChartWidget to access the data in the form fields (e.g. via a $state variable or similar) before the form is submitted (i.e. there is no record in the database table yet)? What I'm trying to do is populate a chart after clicking a "Calculate" button (i.e. Action) on the form and would appreciate any thoughts as to how to approach this. Many thanks.
9 replies
FFilament
Created by w5m on 7/31/2024 in #❓┊help
Custom "Save" button to create or save a form
I'm trying to add a custom "Save" button (i.e. action) to a form.
...
use Filament\Forms\Components\Actions\Action;
...

public static function form(Form $form): Form
{
return $form
->schema([
Section::make('Parameters')
->schema(...)
->footerActions([
Action::make('save')
->label('Save')
->action('save'),
]),
...
...
use Filament\Forms\Components\Actions\Action;
...

public static function form(Form $form): Form
{
return $form
->schema([
Section::make('Parameters')
->schema(...)
->footerActions([
Action::make('save')
->label('Save')
->action('save'),
]),
...
If I pass the string 'save' to the ->action() method, it successfully saves changes to any form fields if I'm Editing a record and click the "Save" button. However, if I'm Creating a record and click the "Save" button, I see an error: Unable to call component method. Public method [save] not found on component. If I pass the string 'create' to the ->action() method, it successfully saves the values in the form fields if I'm Creating a record and click the "Save" button. However, if I'm Editing a record and click the "Save" button, I see an error: Unable to call component method. Public method [create] not found on component . I tried passing a closure to the ->action() method to return the appropriate string depending on the type of form, but this causes nothing to happen on clicking the "Save" button... ->action(fn ($operation) => ($operation === 'edit') ? 'save' : 'create') I'm guessing that ultimately my closure needs to return an object, rather than a string, but I'm not clear what that object should be. Any pointers would be gratefully received.
6 replies
FFilament
Created by w5m on 6/18/2024 in #❓┊help
Setting model attribute based on form field value
If I create a private my_people_count variable in my Example model and a peopleCount() method (return type of Attribute) which gets & sets the private variable. If I also include in my ExampleResource class the following form component: TextInput::make('people_count'), ...what else do I need to do to cause Filament to call the attribute setter when processing the submitted form? (I forgot to point out that the count will not be saved to the underlying database table - it will only be used to indicate how many related model instances to create)
2 replies
FFilament
Created by w5m on 4/23/2024 in #❓┊help
Sortable TextColumn based on Enum labels rather than query
I'm currently using a TaskStatus enum which implements the HasLabel contract. In this enum I've defined an orderedLabels() method to return an array of enum cases ordered by the corresponding labels. I've successfully utilised the enum in a form and in a table... TextColumn::make('task_status_id') ->label('Status') ->sortable() ->badge(), What I would ideally like to do is pass something into the sortable() method of the TextColumn which allows me to specify how to order the enum (as it currently orders by the task_status_id DB table field, an integer, which corresponds to an int-backed enum case). Other than backing the enum cases with integers which match alphabetical order, is there a way I could specify that sorting for this column should be done in alphabetical order of enum labels rather than via a query?
1 replies