F
Filament2y ago
eazy

Dynamic text input value

I have the following form:
return [
TextInput::make('year')
->label(__('Year'))
->numeric()
->reactive()
->required(),
TextInput::make('name')
->label(__('Name'))
->numeric()
->reactive()
->required(),
TextInput::make('code')
->reactive()
->afterStateHydrated(function (TextInput $component, Closure $get) {
$year = $get('year');
$name = $get('name');
if ($year !== null && $name !== null) {
$component->state('xxx');
}
$component->state('123');
})
->disabled()
];
return [
TextInput::make('year')
->label(__('Year'))
->numeric()
->reactive()
->required(),
TextInput::make('name')
->label(__('Name'))
->numeric()
->reactive()
->required(),
TextInput::make('code')
->reactive()
->afterStateHydrated(function (TextInput $component, Closure $get) {
$year = $get('year');
$name = $get('name');
if ($year !== null && $name !== null) {
$component->state('xxx');
}
$component->state('123');
})
->disabled()
];
I want my code field's value change when the year and name input gets a value. If the year and name input is empty I want to show nothing. How can I achieve this?
6 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
eazy
eazyOP2y ago
Doesn't seem to work
Bezhan
Bezhan2y ago
dude your doing it in reverse.. it should be afterStateUpdate() but you need to change the state from year and/or name to trigger the state change. fields can't listen to other fields' state changes only there own but you can change a field's state from another field.
Dan Harrin
Dan Harrin2y ago
yeah afterStateHydrated() only runs when the form is loaded
eazy
eazyOP2y ago
Okay I'm gonna try this, thanks.
Bezhan
Bezhan2y ago
here you go... just adjust how you wanna manipulate it. don't try... just move on to the next thing 🖖
Forms\Components\TextInput::make('year')
->label(__('Year'))
->numeric()
->reactive()
->afterStateUpdated(function (callable $set, callable $get, $state) {
if (filled($name = $get('name'))) {
$set('code', str($state)->append('_')->append($name));
} else {
$set('code', $state);
}
})
->required(),
Forms\Components\TextInput::make('name')
->label(__('Name'))
->reactive()
->afterStateUpdated(function (callable $set, callable $get, $state) {
if (filled($year = $get('year'))) {
$set('code', str($year)->append('_')->append($state));
} else {
$set('code', $state);
}
})
->required(),
Forms\Components\TextInput::make('code')
->disabled()
]);
Forms\Components\TextInput::make('year')
->label(__('Year'))
->numeric()
->reactive()
->afterStateUpdated(function (callable $set, callable $get, $state) {
if (filled($name = $get('name'))) {
$set('code', str($state)->append('_')->append($name));
} else {
$set('code', $state);
}
})
->required(),
Forms\Components\TextInput::make('name')
->label(__('Name'))
->reactive()
->afterStateUpdated(function (callable $set, callable $get, $state) {
if (filled($year = $get('year'))) {
$set('code', str($year)->append('_')->append($state));
} else {
$set('code', $state);
}
})
->required(),
Forms\Components\TextInput::make('code')
->disabled()
]);

Did you find this page helpful?