F
Filament5mo ago
Tjiel

Updating placeholder text afterStateUpdated

Hey all, I currently have. a form where the user can optinally a select a relationship. If the relationship is present it will use the value of the fields from that relationship. Unless the corresponding field is filled in the current form. The problem is that i want to display the values of the relation to the user but not fill the field to do that. So I want to change the placeholder after the state update. currently i have the following:
Select::make('product_id')
->label('Product')
->translateLabel()
->relationship('product', 'name')
->searchable()
->preload()
->live()
->afterStateUpdated(function (callable $set, $state) {
$product = Product::find($state);
$set('name', $product->name);
Select::make('product_id')
->label('Product')
->translateLabel()
->relationship('product', 'name')
->searchable()
->preload()
->live()
->afterStateUpdated(function (callable $set, $state) {
$product = Product::find($state);
$set('name', $product->name);
At the moment it just updates the value. What i want instead is that it will update the placeholder of the name field instead. Is there any way to do this?
4 Replies
BKF Dev
BKF Dev5mo ago
TextInput::make('name') ->placeholder(fn (Get $get) => $get('product_id') ? 'yes' : 'no')
Wrax
Wrax5mo ago
Select::make('product_id')
->relationship('product', 'name')
->live()
->afterStateUpdated(fn (callable $set, $state) =>
$set('relatedProductName', optional(Product::find($state))->name)
),

TextInput::make('name')
->placeholder(fn ($get) => $get('relatedProductName') ?? 'Enter name')
->afterStateHydrated(fn (callable $set, $state) =>
$set('relatedProductName', null)
),
Select::make('product_id')
->relationship('product', 'name')
->live()
->afterStateUpdated(fn (callable $set, $state) =>
$set('relatedProductName', optional(Product::find($state))->name)
),

TextInput::make('name')
->placeholder(fn ($get) => $get('relatedProductName') ?? 'Enter name')
->afterStateHydrated(fn (callable $set, $state) =>
$set('relatedProductName', null)
),
Tjiel
TjielOP5mo ago
Ty for the help, I have implemented it the following way:
TextInput::make('name')
->label('Name')
->translateLabel()
->live()
->placeholder(fn (Get $get) => $get('product_id') ? Product::find($get('product_id'))->name : ''),
TextInput::make('name')
->label('Name')
->translateLabel()
->live()
->placeholder(fn (Get $get) => $get('product_id') ? Product::find($get('product_id'))->name : ''),
BKF Dev
BKF Dev5mo ago
No need live(), just the select live
Want results from more Discord servers?
Add your server