© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
3 replies
SLy

Setting default value in input with createOptionForm

The ->default(fn (Get $get): ?int => $get('state_id')) in following code returns $get('state_id') as null, even though I select a value in state_id select, how can I prefill an input in the create createOptionForm modal with the value from another form field?

Forms\Components\Select::make('city_id')
->label('City')
->relationship('city', 'name')
->options(
    fn (Get $get) => $get('state_id')
        ? City::whereStateId($get('state_id'))->pluck('name', 'id')
        : []
)
->createOptionForm([
    Forms\Components\TextInput::make('state_id')
        ->default(fn (Get $get): ?int => $get('state_id'))
        ->required(),
    Forms\Components\TextInput::make('name')
        ->required(),
])
->preload()
->live()
->required(),
]),
Forms\Components\Select::make('city_id')
->label('City')
->relationship('city', 'name')
->options(
    fn (Get $get) => $get('state_id')
        ? City::whereStateId($get('state_id'))->pluck('name', 'id')
        : []
)
->createOptionForm([
    Forms\Components\TextInput::make('state_id')
        ->default(fn (Get $get): ?int => $get('state_id'))
        ->required(),
    Forms\Components\TextInput::make('name')
        ->required(),
])
->preload()
->live()
->required(),
]),
Solution
That's because the $get in that context is the form in the createOptionForm modal, not the parent form. So you are just getting the same state_id you are trying to set a default on.

I don't know if this will work, but you could try something like ...

->createOptionForm(function (Get $get) {
  return [
    Forms\Components\TextInput::make('state_id')
        ->default($get('state_id'))
        ->required(),
    Forms\Components\TextInput::make('name')
        ->required(),
  ];
  }
)
->createOptionForm(function (Get $get) {
  return [
    Forms\Components\TextInput::make('state_id')
        ->default($get('state_id'))
        ->required(),
    Forms\Components\TextInput::make('name')
        ->required(),
  ];
  }
)
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Default value in Select input
FilamentFFilament / ❓┊help
2y ago
Save default input value in DDBB
FilamentFFilament / ❓┊help
3y ago
How to edit default input value
FilamentFFilament / ❓┊help
2y ago