Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

myregistration's avatar

How do you set an initial Select menu option for a table's CreateAction?

I've tried using mutateFormDataBeforeFill, beforeFill, afterFill in the relative CreateRecord and I've also tried before, beforeFormFilled, afterFormFilled in table's CreateAction. I even tried hard-coding the $data array value for the select menu and created a test select menu, but nothing is working. Are we supposed to set the ->default() value in the select menu? I would think mutating the $data array would set the initial select menu option. If I dd($data) in the beforeFormFilled or afterFormFilled methods I can see the value is being set. Thanks

Here's a test select menu I created in a Filament Resource class to test the relative lifecycle methods:

public static function form(Form $form): Form
{
	return $form
            ->schema([

                Select::make('category')
                    ->options([
                        'option1' => 'Option 1',
                        'option2' => 'Option 2',
                        'option3' => 'Option 3',
                    ])->default('option1')
                    ->required(),
public static function table(Table $table): Table
{
	return $table
	    ->headerActions([
		    Tables\Actions\CreateAction::make()
    		    ->beforeFormFilled(function (array $data, Component $livewire): array {

        		    $data['category'] = 'option2';

        		    return $data;
   		     }),
0 likes
2 replies
LaryAI's avatar
Level 58

To set an initial value for a Select menu in a Filament Resource class, you can indeed use the default() method directly on the Select field. However, if you want to dynamically set the initial value using lifecycle hooks, you should ensure that the hooks are correctly implemented.

Here's a refined approach to ensure that the initial value is set correctly:

  1. Set the default value directly in the Select field:

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Select::make('category')
                    ->options([
                        'option1' => 'Option 1',
                        'option2' => 'Option 2',
                        'option3' => 'Option 3',
                    ])
                    ->default('option1') // Set the default value here
                    ->required(),
            ]);
    }
    
  2. Use the beforeFormFilled hook in the CreateAction to dynamically set the initial value:

    public static function table(Table $table): Table
    {
        return $table
            ->headerActions([
                Tables\Actions\CreateAction::make()
                    ->beforeFormFilled(function (array $data, Component $livewire): array {
                        $data['category'] = 'option2'; // Set the initial value dynamically
                        return $data;
                    }),
            ]);
    }
    
  3. Ensure that the beforeFormFilled hook is correctly placed and returning the modified data:

    public static function table(Table $table): Table
    {
        return $table
            ->headerActions([
                Tables\Actions\CreateAction::make()
                    ->beforeFormFilled(function (array $data, Component $livewire): array {
                        $data['category'] = 'option2'; // Set the initial value dynamically
                        return $data;
                    }),
            ]);
    }
    

By setting the default value directly in the Select field, you ensure that the form has an initial value when it is rendered. Additionally, using the beforeFormFilled hook allows you to dynamically set or override the initial value before the form is filled.

If you still encounter issues, make sure that the lifecycle hooks are being executed as expected by adding debugging statements (e.g., dd($data)) to verify the data at each stage. This will help you identify where the value might be getting lost or overridden.

myregistration's avatar

@LaryAI I have used the dd($data) in beforeFormFilled() method and the data array is getting set, but the select menus aren't.

Please or to participate in this conversation.