benlumley
benlumley
FFilament
Created by benlumley on 2/21/2025 in #❓┊help
Is there a way to avoid duplicate afterStateUpdated methods ?
I have code like:
TextInput::make('number 1')
->live()
->afterStateUpdated(function (Get $get, Set $set): void {
$set('total', $get('number 1') + $get('number 2'));
})
->numeric(),
TextInput::make('number 2')
->live()
->afterStateUpdated(function (Get $get, Set $set): void {
$set('total', $get('number 1') + $get('number 2'));
})
->numeric(),
TextInput::make('total')
->numeric(),
TextInput::make('number 1')
->live()
->afterStateUpdated(function (Get $get, Set $set): void {
$set('total', $get('number 1') + $get('number 2'));
})
->numeric(),
TextInput::make('number 2')
->live()
->afterStateUpdated(function (Get $get, Set $set): void {
$set('total', $get('number 1') + $get('number 2'));
})
->numeric(),
TextInput::make('total')
->numeric(),
Simplified example; but basically, one field depends on two others. You have to have the same duplicated afterStateUpdated callback on both fields. It'd be a load nicer not to duplicate this and just have it attached to the total field. Is there a way to do this at all ? I've had a good dig but can't find anything. (I realise I can extract the method to a closure/other method; but it'd still be nice not to attach it twice)
7 replies