F
Filament2w ago
btx

Toggle default true

Seems toggle does not accept to be true by default. Bug?
Forms\Components\Toggle::make('billing_use_company_address')
->afterStateHydrated(function (Forms\Components\Toggle $component) {
$component->state(true);
})
->default(true)
->dehydrated(false)
->reactive()
->label('Use company address?');
Forms\Components\Toggle::make('billing_use_company_address')
->afterStateHydrated(function (Forms\Components\Toggle $component) {
$component->state(true);
})
->default(true)
->dehydrated(false)
->reactive()
->label('Use company address?');
7 Replies
LeandroFerreira
default works in the CreatePage
btx
btxOP7d ago
The issue is when creating a new record, unfortunately none of these hooks is triggerd
->afterStateHydrated(function ($state) {
dd($state, 1);
})
->formatStateUsing(function($state) {
dd($state, 2);
})
->default(function ($state) {
dd($state, 3);
})
->afterStateHydrated(function ($state) {
dd($state, 1);
})
->formatStateUsing(function($state) {
dd($state, 2);
})
->default(function ($state) {
dd($state, 3);
})
LeandroFerreira
is it a custom livewire component?
btx
btxOP7d ago
No, just a toggle as first element of a Tab Form, to toggle visibility of the address form.
Forms\Components\Toggle::make('billing_use_company_address')
->extraAttributes(['class' => 'my-4'])
->columnSpanFull()
->afterStateHydrated(function ($state) {
dd($state, 1);
})
->formatStateUsing(function($state) {
dd($state, 2);
})
->default(function ($state) {
dd($state, 3);
})
// ->formatStateUsing(fn($record) => $record->billing_address_id === null)
->dehydrated(false)
->beforeStateDehydrated(function ($state, ?Customer $record) {
if ($state === true && $record && $record->localBillingAddress) {
$address = $record->localBillingAddress;
$record->localBillingAddress()->disassociate()->save();
$address->delete();
}
})
->reactive()
->label('Use company address?'),

Forms\Components\Grid::make(2)
->hidden(fn(Get $get) => $get('billing_use_company_address'))
->relationship('localBillingAddress')
->schema(function (Get $get) {
return AddressResourceForm::getSchema();
})
Forms\Components\Toggle::make('billing_use_company_address')
->extraAttributes(['class' => 'my-4'])
->columnSpanFull()
->afterStateHydrated(function ($state) {
dd($state, 1);
})
->formatStateUsing(function($state) {
dd($state, 2);
})
->default(function ($state) {
dd($state, 3);
})
// ->formatStateUsing(fn($record) => $record->billing_address_id === null)
->dehydrated(false)
->beforeStateDehydrated(function ($state, ?Customer $record) {
if ($state === true && $record && $record->localBillingAddress) {
$address = $record->localBillingAddress;
$record->localBillingAddress()->disassociate()->save();
$address->delete();
}
})
->reactive()
->label('Use company address?'),

Forms\Components\Grid::make(2)
->hidden(fn(Get $get) => $get('billing_use_company_address'))
->relationship('localBillingAddress')
->schema(function (Get $get) {
return AddressResourceForm::getSchema();
})
LeandroFerreira
ok, but are you using the panel builder or only the form builder?
btx
btxOP6d ago
How can I find this? I think panel builder right? I just extracted the fields to an extra class
class CustomerResource extends Resource {
protected static ?string $model = Customer::class;
protected static ?string $navigationIcon = IconEnum::Customers_o;
protected static ?string $navigationGroup = 'Commerce';
protected static ?int $navigationSort = 1;

public static function form(Form $form): Form {
// The form is here
return $form->schema(CustomerResourceForm::getSchema());
}

public static function table(Table $table): Table {
return $table
->columns(CustomerResourceTable::getColumns())
->filters(CustomerResourceFilters::getFilters())
->actions(CustomerResourceActions::getActions())
->bulkActions(CustomerResourceActions::getBulkActions());
}

public static function getPages(): array {
return [
'index' => Pages\ManageCustomers::route('/'),
];
}
}
class CustomerResource extends Resource {
protected static ?string $model = Customer::class;
protected static ?string $navigationIcon = IconEnum::Customers_o;
protected static ?string $navigationGroup = 'Commerce';
protected static ?int $navigationSort = 1;

public static function form(Form $form): Form {
// The form is here
return $form->schema(CustomerResourceForm::getSchema());
}

public static function table(Table $table): Table {
return $table
->columns(CustomerResourceTable::getColumns())
->filters(CustomerResourceFilters::getFilters())
->actions(CustomerResourceActions::getActions())
->bulkActions(CustomerResourceActions::getBulkActions());
}

public static function getPages(): array {
return [
'index' => Pages\ManageCustomers::route('/'),
];
}
}
I found out, that this behavior happens because of the visibility of the surrounding Grid is dependent on another field. I guess it will be best if I make an example repo, because it seems like a bug for me. For now I just inverted all the logic

Did you find this page helpful?