F
Filament7mo ago
Koda

Clear Button in custom form

I try to use a "clear" Button in a custom form
protected function getFormActions(): array
{
return [
Action::make('create')
->label(__('filament-panels::resources/pages/create-record.form.actions.create.label'))
->submit('create'),

Action::make('clear')
->action(function (Set $set) {
$set('data.name', '',);
$set('name', '');
$this->form->fill();
})
];
}
protected function getFormActions(): array
{
return [
Action::make('create')
->label(__('filament-panels::resources/pages/create-record.form.actions.create.label'))
->submit('create'),

Action::make('clear')
->action(function (Set $set) {
$set('data.name', '',);
$set('name', '');
$this->form->fill();
})
];
}
The create Button works, but the clear buton does not.
Solution:
try
Action::make('clear')
->action('clear')
Action::make('clear')
->action('clear')
...
Jump to solution
7 Replies
Tally
Tally7mo ago
Should work... a dd in the clear action is not showing? The action is a \Filament\Actions\Action
Vp
Vp7mo ago
Maybe like this..
Action::make('clear')
->action(fn () => $this->data = [])
Action::make('clear')
->action(fn () => $this->data = [])
Koda
KodaOP7mo ago
@Tally A dd is not showing, and the Action is Filament\Actions\Action
Tally
Tally7mo ago
Weird... just tried your code in a testproject and it just works... no console errors?
Koda
KodaOP7mo ago
No console error. Strange. Maybe a missing implement? Here a shoretd version
<?php

namespace App\Filament\Pages\XXX;

use Filament\Actions\Action;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Forms\Set;
use Filament\Notifications\Notification;
use Filament\Pages\Page;

class CreateXXX extends Page implements HasForms
{
use InteractsWithForms;

public ?array $data = [];

protected static string $view = 'filament.pages.xxx';

public function mount(): void
{
$this->form->fill();
}

public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->autofocus(),
])
->statePath('data');
}

protected function getFormActions(): array
{
return [
Action::make('create')
->label(__('filament-panels::resources/pages/create-record.form.actions.create.label'))
->submit('create'),

Action::make('clear')
->action(function (Set $set) {
dd(1);
$set('data.name', '',);
$set('name', '');
$this->form->fill();
})
];
}

public function create(): void
{
$this->form->fill();
Notification::make()
->success()
->title('OK')
->send();
}
}
<?php

namespace App\Filament\Pages\XXX;

use Filament\Actions\Action;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Forms\Set;
use Filament\Notifications\Notification;
use Filament\Pages\Page;

class CreateXXX extends Page implements HasForms
{
use InteractsWithForms;

public ?array $data = [];

protected static string $view = 'filament.pages.xxx';

public function mount(): void
{
$this->form->fill();
}

public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->autofocus(),
])
->statePath('data');
}

protected function getFormActions(): array
{
return [
Action::make('create')
->label(__('filament-panels::resources/pages/create-record.form.actions.create.label'))
->submit('create'),

Action::make('clear')
->action(function (Set $set) {
dd(1);
$set('data.name', '',);
$set('name', '');
$this->form->fill();
})
];
}

public function create(): void
{
$this->form->fill();
Notification::make()
->success()
->title('OK')
->send();
}
}
<x-filament-panels::page
@class([
'fi-resource-create-record-page',
])
>
<x-filament-panels::form
id="form"
wire:submit="create"
>
{{ $this->form }}

<x-filament-panels::form.actions
:actions="$this->getFormActions()"
/>
</x-filament-panels::form>
</x-filament-panels::page>
<x-filament-panels::page
@class([
'fi-resource-create-record-page',
])
>
<x-filament-panels::form
id="form"
wire:submit="create"
>
{{ $this->form }}

<x-filament-panels::form.actions
:actions="$this->getFormActions()"
/>
</x-filament-panels::form>
</x-filament-panels::page>
Solution
LeandroFerreira
LeandroFerreira7mo ago
try
Action::make('clear')
->action('clear')
Action::make('clear')
->action('clear')
public function clear(): void
{
$this->form->fill();
}
public function clear(): void
{
$this->form->fill();
}
Koda
KodaOP7mo ago
@Leandro Ferreira Thank you. This works great
Want results from more Discord servers?
Add your server