From 562f15964520183f120b78a87bd480b2b5395fb9 Mon Sep 17 00:00:00 2001 From: Nick Breland Date: Wed, 14 Feb 2024 10:30:59 +0000 Subject: [PATCH] fix: decimal format so input works without mask --- src/Forms/Components/MoneyInput.php | 2 +- src/MoneyFormatter.php | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Forms/Components/MoneyInput.php b/src/Forms/Components/MoneyInput.php index 970183f..6770050 100644 --- a/src/Forms/Components/MoneyInput.php +++ b/src/Forms/Components/MoneyInput.php @@ -29,7 +29,7 @@ protected function setUp(): void return $state; } - return MoneyFormatter::format($state, $currency, $locale); + return MoneyFormatter::formatAsDecimal($state, $currency, $locale); }); $this->dehydrateStateUsing(function (MoneyInput $component, $state): string { diff --git a/src/MoneyFormatter.php b/src/MoneyFormatter.php index 7baa818..e13ec8c 100644 --- a/src/MoneyFormatter.php +++ b/src/MoneyFormatter.php @@ -24,6 +24,19 @@ public static function format($value, $currency, $locale, $monetarySeparator = n return $moneyFormatter->format($money); } + public static function formatAsDecimal($value, $currency, $locale): string + { + if (is_null($value) || $value === '') { + return ''; + } + + $numberFormatter = self::getNumberFormatter($locale, \NumberFormatter::DECIMAL); + $moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies()); + + $money = new Money($value, $currency); + return $moneyFormatter->format($money); // outputs 1.000,00 + } + public static function parseDecimal($moneyString, $currency, $locale): string { if (is_null($moneyString) || $moneyString === '') {