F
Filament2w ago
taz

ToggleButtons default not working

When I try to set a default to ToggleButtons form with multiple options, it does not select it.
ToggleButtons::make('technologies')
->default('tailwind')
->multiple()
->options([
'tailwind' => 'Tailwind CSS',
'alpine' => 'Alpine.js',
'laravel' => 'Laravel',
'livewire' => 'Laravel Livewire',
]),
ToggleButtons::make('technologies')
->default('tailwind')
->multiple()
->options([
'tailwind' => 'Tailwind CSS',
'alpine' => 'Alpine.js',
'laravel' => 'Laravel',
'livewire' => 'Laravel Livewire',
]),
4 Replies
LeandroFerreira
Did you add $this->form->fill() in the mount method?
taz
tazOP2w ago
It works but I noticed that this piece of code messed up with setting the default state:
class ToggleButtons extends Field implements Contracts\CanDisableOptions
{
use Concerns\CanDisableOptions;
use Concerns\CanDisableOptionsWhenSelectedInSiblingRepeaterItems;
use Concerns\CanFixIndistinctState;
use Concerns\HasColors;
use Concerns\HasExtraInputAttributes;
use Concerns\HasGridDirection;
use Concerns\HasIcons;
use Concerns\HasOptions;

public const GROUPED_VIEW = 'filament-forms::components.toggle-buttons.grouped';

protected bool | Closure $isMultiple = false;

/**
* @var view-string
*/
protected string $view = 'filament-forms::components.toggle-buttons.index';

protected bool | Closure $isInline = false;

protected function setUp(): void
{
parent::setUp();

$this->default(fn (ToggleButtons $component): mixed => $component->isMultiple() ? [] : null);

$this->afterStateHydrated(static function (ToggleButtons $component, $state): void {
if (! $component->isMultiple()) {
return;
}

if (is_array($state)) {
return;
}

$component->state([]);
});
}
class ToggleButtons extends Field implements Contracts\CanDisableOptions
{
use Concerns\CanDisableOptions;
use Concerns\CanDisableOptionsWhenSelectedInSiblingRepeaterItems;
use Concerns\CanFixIndistinctState;
use Concerns\HasColors;
use Concerns\HasExtraInputAttributes;
use Concerns\HasGridDirection;
use Concerns\HasIcons;
use Concerns\HasOptions;

public const GROUPED_VIEW = 'filament-forms::components.toggle-buttons.grouped';

protected bool | Closure $isMultiple = false;

/**
* @var view-string
*/
protected string $view = 'filament-forms::components.toggle-buttons.index';

protected bool | Closure $isInline = false;

protected function setUp(): void
{
parent::setUp();

$this->default(fn (ToggleButtons $component): mixed => $component->isMultiple() ? [] : null);

$this->afterStateHydrated(static function (ToggleButtons $component, $state): void {
if (! $component->isMultiple()) {
return;
}

if (is_array($state)) {
return;
}

$component->state([]);
});
}
afterStateHydrated is called on the component during its first lifecycle and $state is null so it sets $component->state([]) at the end of the function to an empty array overrding defaut state defined in the components builder
php ToggleButtons::make('technologies')
->default('tailwind')
php ToggleButtons::make('technologies')
->default('tailwind')
Hitesh Makwana
Forms\Components\ToggleButtons::make('technologies')
->default(['tailwind', 'alpine'])
->multiple()
->options([
'tailwind' => 'Tailwind CSS',
'alpine' => 'Alpine.js',
'laravel' => 'Laravel',
'livewire' => 'Laravel Livewire',
])
Forms\Components\ToggleButtons::make('technologies')
->default(['tailwind', 'alpine'])
->multiple()
->options([
'tailwind' => 'Tailwind CSS',
'alpine' => 'Alpine.js',
'laravel' => 'Laravel',
'livewire' => 'Laravel Livewire',
])
use default data in array because you use multiple
taz
tazOP2w ago
Okay works perfectly in a tradictional Form. Im in a filtersForm.
php public function filtersForm(Form $form): Form
php public function filtersForm(Form $form): Form
Might be an issue. I will investigate more. This is definitely an issue. When using ToggleButtons within a filtersForm, setting defaults does not work.

Did you find this page helpful?