Skip to content

Commit

Permalink
fix: decimal format so input works without mask
Browse files Browse the repository at this point in the history
  • Loading branch information
npbreland committed Feb 14, 2024
1 parent 5b20464 commit 562f159
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Forms/Components/MoneyInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions src/MoneyFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 === '') {
Expand Down

0 comments on commit 562f159

Please sign in to comment.