Skip to content

Instantly share code, notes, and snippets.

@bilogic
Last active September 30, 2023 08:09
Show Gist options
  • Save bilogic/3c8d9bb37e3a81e319fb2e5b8b8d364a to your computer and use it in GitHub Desktop.
Save bilogic/3c8d9bb37e3a81e319fb2e5b8b8d364a to your computer and use it in GitHub Desktop.
Testing Filament Actions
<x-filament-panels::page>
@foreach ($this->getForms() as $form)
<x-filament-panels::form wire:submit="formUpdate('{{ $form }}')">
{{ $this->$form }}
<x-filament-panels::form.actions :actions="$this->getFormActions($form)" />
</x-filament-panels::form>
@endforeach
</x-filament-panels::page>
<?php
namespace App\Filament\Pages;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Pages\Actions\Action;
use Filament\Pages\Page;
use Filament\Support\Exceptions\Halt;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
class Test extends Page implements HasForms
{
use InteractsWithForms;
protected static string $view = 'filament.pages.page-with-forms';
protected function getFormActions($form): array
{
if (method_exists($this, "{$form}Actions")) {
return $this->{"{$form}Actions"}($form);
} else {
return [
Action::make($form.'UpdateAction')
->label(__('filament-panels::pages/auth/edit-profile.form.actions.save.label'))
->submit($form),
];
}
}
public function formUpdate($form): void
{
try {
$data = $this->{$form}->getState();
if (method_exists($this, "{$form}Save")) {
$this->{"{$form}Save"}($form);
} else {
$loadRecordMethod = "{$form}Load";
$this->handleRecordUpdate($this->{$loadRecordMethod}(), $data);
}
} catch (Halt $exception) {
return;
}
$this->sendSuccessNotification();
}
protected function handleRecordUpdate(Model $record, array $data): Model
{
$record->update($data);
return $record;
}
protected function sendSuccessNotification(): void
{
Notification::make()
->success()
->title(__('filament-panels::pages/auth/edit-profile.notifications.saved.title'))
->send();
}
public function mount(): void
{
foreach ($this->getForms() as $form) {
$loadRecordMethod = "{$form}Load";
$record = $this->{$loadRecordMethod}();
if (is_array($record)) {
$this->{$form}->fill($record);
return;
}
if ($record) {
$this->{$form}->fill($record->toArray());
}
}
}
protected static ?string $navigationGroup = 'Sales';
public function getTitle(): string|Htmlable
{
return __('Test');
}
protected function getHeaderActions(): array
{
return [];
}
protected function getForms(): array
{
return [
'testForm',
];
}
public ?array $testFormData = [];
public function testFormLoad(): ?Model
{
// do something to load and return a model
return null;
}
private function testForm(Form $form): Form
{
return $form
->model($this->testFormLoad())
->statePath('testFormData')
->schema([
Section::make('Part 1')
->aside()
->description('Link your account.')
->schema([
TextInput::make('email')
->label(__('Primary Email Address'))
->email()
->readOnly()
->unique(ignoreRecord: true),
]),
]);
}
public function performTest(Request $request)
{
// Method App\Filament\Business\Pages\Test::notify does not exist.
$this->notify('success', 'Your test was successful!');
// Call to a member function get() on null
dd($request->arguments->get());
}
protected function testFormActions($form): array
{
$actions[] = \Filament\Actions\Action::make('Test')
->label(__('Test'))
->color('gray')
->action('performTest')
->arguments(['option1']);
return $actions;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment