F
Filamentβ€’17mo 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 Userβ€’17mo ago
Message Not Public
Sign In & Join Server To View
eazy
eazyβ€’17mo ago
Doesn't seem to work
Bezhan
Bezhanβ€’17mo 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 Harrinβ€’17mo ago
yeah afterStateHydrated() only runs when the form is loaded
eazy
eazyβ€’17mo ago
Okay I'm gonna try this, thanks.
Bezhan
Bezhanβ€’17mo 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()
]);
Want results from more Discord servers?
Add your server
More Posts
Accessing the current model inside custom validation ruleHi. Is there a way to access the current model value inside the custom validation function? ```php Custom AlpineJS validationI'm using the Filament - and my form is like this: ``` Forms\Components\Toggle::make('profile_data_DatePicker creating records wrongly but editing correctlyI have an app configured with `UTC` timezone, and form with the following date picker (DATE ONLY, noHow do I add "forgot password" link to login page?How do I add "forgot password" link to login page?Grouping form fields to reuse them in different formsIs it possible to make a group a f.e. 5 form fields and save them in a "file". And reuse them in sevIs there a way to auto-scrub model attributes that aren't being used?Hello, according to https://filamentphp.com/docs/2.x/admin/resources/security, filament automaticallhow to make uploaded image only visible inside the filament admin panelI have a filament form outside the admin panel where fotos can be uploaded. What do i have to do thaMissing sidebar menu nav``` Alpine Expression Error: Cannot read properties of null (reading 'includes') Expression: "! ($sIs there a way to display no records in the list view by default and only see records when searchin?I want to make it so that: User accesses list records page -> User sees 0 records by default -> UsemutateFormDataBeforeCreate vs dehydrateStateUsingAre they kind of interchangeable? I understand that one is specific to create, but in general, could