Home / Symfony / New in Symfony 7.4: Better Currency Filtering

New in Symfony 7.4: Better Currency Filtering


Thibault Gattolliat
Contributed by
Thibault Gattolliat
in
#61431
and #61837

By default, the Currencies class from the Intl component and the
CurrencyType field from the Form component display all currencies defined
in the ISO 4217 standard.

This list includes legacy currencies such as the German Mark or the Irish Pound,
which were replaced by the Euro decades ago. For e-commerce or billing applications,
showing obsolete currencies makes no sense and creates a poor user experience.

Symfony 7.4 solves this by filtering out legacy currencies by default and
introducing new methods to check and filter currencies based on their legal
tender status and validity dates.

New Intl Helpers

The Currencies class now includes methods to filter currencies by country
and date, using ICU metadata to determine their legal tender status and
validity period:

use SymfonyComponentIntlCurrencies;

// get all today's legal and active currencies for a given country
$currencies = Currencies::forCountry('FR');
// Result: ['EUR']

// get all valid currencies for a given country at a specific date
$currencies = Currencies::forCountry(
    'ES',
    legalTender: null,
    active: true,
    date: new DateTimeImmutable('1982-01-01')
);
// Result: ['ESP', 'ESB']

// validate if a currency is valid for a given country
$isValid = Currencies::isValidInCountry('CH', 'CHF');
// Result: true

New Form Type Options

The CurrencyType form field now includes three new options to filter the
displayed currencies:

  • legal_tender: (default: true in 7.4 and null in previous versions)
    Controls whether to show only legal tender currencies (true), only
    non-legal-tender currencies (false), or all currencies (null).
  • active_at: Shows only currencies that were active (in circulation) at a
    specific date.
  • not_active_at: Shows only currencies that were inactive (no longer in
    circulation) at a specific date.

These options help you build currency selectors that only show relevant choices
to your users:

$builder->add('currency', CurrencyType::class, [
    'legal_tender' => null,
    'active_at' => new DateTimeImmutable('2007-01-15', new DateTimeZone('Etc/UTC')),
]);


Sponsor the Symfony project.
Tagged:

Leave a Reply

Your email address will not be published. Required fields are marked *