Default value in Select input

I want to set one value as default, but the default() is not working. I have had this problem many times. Is there an issue with Select or am I doing something wrong ? php Select::make('date_format') ->options([ 'm/d/Y' => date('m/d/Y'), // Key is the format, value is the formatted date 'n/j/y' => date('n/j/y'), 'F j, Y' => date('F j, Y'), 'M j, Y' => date('M j, Y'), ]) ->default('m/d/Y') // Set the key as the default, which corresponds to the formatted date ->label('Date Format'), this is my code.
Solution:
SettingsPage is not a CreatePage though. I uses already existing data.
Jump to solution
17 Replies
Dennis Koch
Dennis Koch3mo ago
Is this on a Create or Edit page?
Dennis Koch
Dennis Koch3mo ago
Note that these defaults are only used when the form is loaded without existing data. Inside panel resources this only works on Create Pages, as Edit Pages will always fill the data from the model.
https://filamentphp.com/docs/3.x/forms/fields/getting-started#setting-a-default-value
namrata
namrata3mo ago
Create Page
Dennis Koch
Dennis Koch3mo ago
Copied your code an this is what I get.
No description
namrata
namrata3mo ago
The default is being set ?
namrata
namrata3mo ago
It is not in mine.
No description
Dennis Koch
Dennis Koch3mo ago
What version are you using? Can you run php artisan about --only=filament?
namrata
namrata3mo ago
No description
Dennis Koch
Dennis Koch3mo ago
Can you try upgrading to the latest version and see whether that changes anything?
namrata
namrata3mo ago
Sure. - Upgrading filament/filament (v3.2.92 => v3.2.98) but still not working This ia actually not in resource but in page Could that be causing issues ?
Kiran Timsina
Kiran Timsina3mo ago
I tried your code. Strange. There's not default set. It's "Select an option" in my.
namrata
namrata3mo ago
Yes, it is working in the resource, but not in Page.
No description
Dennis Koch
Dennis Koch3mo ago
Custom page? Can you share the code?
namrata
namrata3mo ago
<?php

namespace App\Filament\Pages;

use App\Settings\SiteSettings;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Form;
use Filament\Pages\SettingsPage;

class ManageSite extends SettingsPage
{
protected static ?string $navigationIcon = 'heroicon-o-cog-6-tooth';

protected static string $settings = SiteSettings::class;

public function form(Form $form): Form
{
return $form
->schema([
Section::make(' Settings')
->schema([

Select::make('date_format')
->options([
'm/d/Y' => date('m/d/Y'),
'n/j/y' => date('n/j/y'),
'F j, Y' => date('F j, Y'),
'M j, Y' => date('M j, Y'),
])
->default('m/d/Y')
->label('Date Format'),

Select::make('time_format')
->options([
'g:i:s a' => date('g:i:s a'),
'g:i:s A' => date('g:i:s A'),
'H:i:s' => date('H:i:s'),
]),



]),
]);
}
}
<?php

namespace App\Filament\Pages;

use App\Settings\SiteSettings;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Form;
use Filament\Pages\SettingsPage;

class ManageSite extends SettingsPage
{
protected static ?string $navigationIcon = 'heroicon-o-cog-6-tooth';

protected static string $settings = SiteSettings::class;

public function form(Form $form): Form
{
return $form
->schema([
Section::make(' Settings')
->schema([

Select::make('date_format')
->options([
'm/d/Y' => date('m/d/Y'),
'n/j/y' => date('n/j/y'),
'F j, Y' => date('F j, Y'),
'M j, Y' => date('M j, Y'),
])
->default('m/d/Y')
->label('Date Format'),

Select::make('time_format')
->options([
'g:i:s a' => date('g:i:s a'),
'g:i:s A' => date('g:i:s A'),
'H:i:s' => date('H:i:s'),
]),



]),
]);
}
}
this is my code, This extends settings page, which extends page
Solution
Dennis Koch
Dennis Koch3mo ago
SettingsPage is not a CreatePage though. I uses already existing data.
Dennis Koch
Dennis Koch3mo ago
If you want to mutate data you need to use the mutateDataBeforeFill() hook
namrata
namrata3mo ago
Ohh!! Thanks a lot
Want results from more Discord servers?
Add your server