Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for dynamically updating currency and locale of MoneyInput #42

Open
AlexanderBV opened this issue Jun 26, 2024 · 5 comments
Open
Labels
enhancement New feature or request
Milestone

Comments

@AlexanderBV
Copy link

Describe the bug
I'm encountering an error when trying to dynamically set the currency and locale for the MoneyInput component in Filament forms. The error occurs specifically when trying to use a closure for the currency property.

Error Message
Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization

The error is triggered by this line:

->currency(fn ($get) => $get('unit_price_currency'))

Steps To Reproduce:

```php
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Select;
use Pelmered\FilamentMoneyField\Forms\Components\MoneyInput;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Forms\Set;
use App\Models\Catalog;
use App\Enums\RateType;

public function form(Form $form): Form
{
    return $form
        ->schema([
            Select::make('currency_id')
                ->label('Moneda')
                ->relationship('currency', 'name')
                ->searchable(['code', 'name'])
                ->preload()
                ->reactive()
                ->required()
                ->afterStateUpdated(function (Set $set, Get $get) {
                    $currency = Catalog::find($get('currency_id'));
                    $set('unit_price_currency', $currency->code);
                    $set('unit_price_locale', $currency->properties['locale'] ?? 'es_PE');
                })
                ->afterStateHydrated(function (Set $set, Get $get, $state) {
                    $currency = Catalog::find($state);
                    $set('unit_price_currency', $currency->code);
                    $set('unit_price_locale', $currency->properties['locale'] ?? 'es_PE');
                }),
            Hidden::make('unit_price_currency'),
            Hidden::make('unit_price_locale'),
            MoneyInput::make('unit_price')
                ->label('Precio')
                ->currency(fn ($get) => $get('unit_price_currency'))
                ->locale(fn ($get) => $get('unit_price_locale'))
        ]);
}

Changes have in global config:

// .env file settings
MONEY_DEFAULT_LOCALE=es_PE
MONEY_DEFAULT_CURRENCY=PEN

// Configuration published with
php artisan vendor:publish --provider="Pelmered\FilamentMoneyField\FilamentMoneyFieldServiceProvider" --tag="config"

Expected behavior and actual behavior
I expected the MoneyInput component to correctly use the dynamically set currency and locale values without causing initialization errors. Instead, it throws an error indicating that the property $container must not be accessed before initialization.

Screenshots
Screenshot from 2024-06-26 11-31-49

Additional context
I believe the issue might be related to the initialization of the $container property, which seems to be accessed before it is fully initialized. Any insights or solutions to this problem would be greatly appreciated. Thank you for your assistance!

Version of:
This package: Latest
Filament: 3.2
PHP: 8.2
Laravel: 10.10

@pelmered
Copy link
Owner

Thank you for the report! I will investigate this today.

@pelmered
Copy link
Owner

I don't believe this error has anything to with this plugin. It is how Filament works.

I think you have to do something like this:

                MoneyInput::make('price')
                          ->label('Precio')
                          ->afterStateUpdated(function (MoneyInput $component, bool $state) {
                              $component->currency(fn($get) => $get('unit_price_currency') ?? 'USD')
                                        ->locale(fn($get) => $get('unit_price_locale') ?? 'es_PE');
                          })

@pelmered
Copy link
Owner

I have now tested some, and I'm not sure how well it works to change the field dynamically like this. This is something I will have to look into in the future. For now I would probably just use a currency select and a normal input without localized format. For displaying it should work with the MoneyColumn and the MoneyEntry.

If you find a way to support this, please let me know.

@pelmered pelmered changed the title Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization Support for dynamically updating currency and locale of MoneyInput Jul 24, 2024
@pelmered pelmered added the enhancement New feature or request label Jul 25, 2024
@bilogic
Copy link

bilogic commented Aug 11, 2024

I think this issue will be addressed if we figure out how prices should be persisted. My view of s price is that it should always consist of a currency and value.

Is there a recommended way? Especially if there are multiple prices in record.

@pelmered
Copy link
Owner

pelmered commented Nov 7, 2024

I think this issue will be addressed if we figure out how prices should be persisted. My view of s price is that it should always consist of a currency and value.

Is there a recommended way? Especially if there are multiple prices in record.

Yes, I would agree with that!
I'm not sure how that would be implemented and stored in the database. I guess it has to be separate columns. This is something I would like to work on for a 2.0 release. I you have ideas I'm happy for feedback and ideas on how to do this.

I guess the best way to do this is a integer/decimal column for amount and one char(3) for currency. We should add a migration type for this like for example the morph type that created two related columns.

@pelmered pelmered added this to the 2.0 milestone Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants