F
Filamentβ€’2y ago
gizmojo

Set value of another field from within default

Can I set the value of the key from within the default of the name. Ideally I don't want to duplicate the logic fetching the default name.
Forms\Components\TextInput::make('name')
->afterStateUpdated(function (\Closure $set, ?string $state):void {
$set('key', Str::slug($state));
})
->default(function (\Closure $set): string {
$name = 'Complicated logic';
$set('key', Str::slug($name));
return $name;
})
->reactive()
->required(),

Forms\Components\TextInput::make('key'),
Forms\Components\TextInput::make('name')
->afterStateUpdated(function (\Closure $set, ?string $state):void {
$set('key', Str::slug($state));
})
->default(function (\Closure $set): string {
$name = 'Complicated logic';
$set('key', Str::slug($name));
return $name;
})
->reactive()
->required(),

Forms\Components\TextInput::make('key'),
If you put the key input first it works. I think the default is getting called before all the fields are registered is there a way around this?
5 Replies
Patrick Boivin
Patrick Boivinβ€’2y ago
Possibly afterStateHydrated() instead of default()
gizmojo
gizmojoOPβ€’2y ago
Tried that but unfortunately the key value is not getting set. I'm using those fields inside createOptionForm and I don't think there's an afterFill hook equivalent.
Patrick Boivin
Patrick Boivinβ€’2y ago
lol, I was just going to suggest afterFill Can you subclass the Select field and override getCreateOptionAction()? I think you would be able to add a afterFormFilled() hook to it
gizmojo
gizmojoOPβ€’2y ago
You've led me to modifyCreateOptionActionUsing just going to see if I can get that working. That wasn't working but I figured I could get the value instead. Spent too long trying to get it to work the other way around.
Forms\Components\TextInput::make('key')
->default(fn (\Closure $get) => Str::slug($get('name')))
Forms\Components\TextInput::make('key')
->default(fn (\Closure $get) => Str::slug($get('name')))
Patrick Boivin
Patrick Boivinβ€’2y ago
Nice and simple πŸ‘Œ
Want results from more Discord servers?
Add your server