Price (in pence) field - show result in pounds in suffix/or somewhere else that's sensible

Hi, I am attempting to let a user enter a price in pence, and then show them the converted amount in pounds in a clean and simple way. I hace tried the following but unfortunately the suffix doesn't get updated.
Forms\Components\TextInput::make('price')
->label('Price (in pence)')
->numeric()
->live()
->suffix(function($record) {
return '£'.number_format($record->price / 100, 2);
}),
Forms\Components\TextInput::make('price')
->label('Price (in pence)')
->numeric()
->live()
->suffix(function($record) {
return '£'.number_format($record->price / 100, 2);
}),
Any ideas on how I could achieve someting like this? Thanks!
4 Replies
Povilas K
Povilas K2mo ago
We had this tutorial: https://laraveldaily.com/post/filament-input-prefix-suffix-dynamic-based-on-other-input - not specifically your case because it's for MULTIPLE fields, but maybe would work for you, too, please try
ModestasV
ModestasV2mo ago
@Ralph Morris A working example combining ^ tutorial and your code:
Forms\Components\TextInput::make('price')
->label('Price (in cents)')
->numeric()
->live()
->suffix(function (Forms\Get $get) {
return 'EUR ' . number_format($get('price') / 100, 2);
})
->required(),
Forms\Components\TextInput::make('price')
->label('Price (in cents)')
->numeric()
->live()
->suffix(function (Forms\Get $get) {
return 'EUR ' . number_format($get('price') / 100, 2);
})
->required(),
You need to use Filament form Get instead of record, as your record might not yet exist on create.
Adam Holmes
Adam Holmes2mo ago
I use a plugin for this: https://filamentphp.com/plugins/pelmered-money-field User enters an amount in £ (or your chosen currency) and then it converts to pennies on save.
Filament
Money field by Peter Elmered - Filament
Filament field for money powered by Money PHP.
Ralph Morris
Ralph MorrisOP2mo ago
Worked an absolute charm. Thank you all!
Want results from more Discord servers?
Add your server