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 Koch4mo ago
Is this on a Create or Edit page?
Dennis Koch
Dennis Koch4mo 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
namrataOP4mo ago
Create Page
Dennis Koch
Dennis Koch4mo ago
Copied your code an this is what I get.
No description
namrata
namrataOP4mo ago
The default is being set ?
namrata
namrataOP4mo ago
It is not in mine.
No description
Dennis Koch
Dennis Koch4mo ago
What version are you using? Can you run php artisan about --only=filament?
namrata
namrataOP4mo ago
No description
Dennis Koch
Dennis Koch4mo ago
Can you try upgrading to the latest version and see whether that changes anything?
namrata
namrataOP4mo 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 Timsina4mo ago
I tried your code. Strange. There's not default set. It's "Select an option" in my.
namrata
namrataOP4mo ago
Yes, it is working in the resource, but not in Page.
No description
Dennis Koch
Dennis Koch4mo ago
Custom page? Can you share the code?
namrata
namrataOP4mo 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 Koch4mo ago
SettingsPage is not a CreatePage though. I uses already existing data.
Dennis Koch
Dennis Koch4mo ago
If you want to mutate data you need to use the mutateDataBeforeFill() hook
namrata
namrataOP4mo ago
Ohh!! Thanks a lot
Want results from more Discord servers?
Add your server