Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bf3e358

Browse filesBrowse files
committed
[Form] Fixed creation of multiple money fields with different currencies
1 parent 12ad992 commit bf3e358
Copy full SHA for bf3e358

File tree

Expand file treeCollapse file tree

2 files changed

+24
-8
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+24
-8
lines changed

‎src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php
+10-8Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,14 @@ private static function getPattern($currency)
8686
return '{{ widget }}';
8787
}
8888

89-
if (!isset(self::$patterns[\Locale::getDefault()])) {
90-
self::$patterns[\Locale::getDefault()] = array();
89+
$locale = \Locale::getDefault();
90+
91+
if (!isset(self::$patterns[$locale])) {
92+
self::$patterns[$locale] = array();
9193
}
9294

93-
if (!isset(self::$patterns[\Locale::getDefault()][$currency])) {
94-
$format = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::CURRENCY);
95+
if (!isset(self::$patterns[$locale][$currency])) {
96+
$format = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
9597
$pattern = $format->formatCurrency('123', $currency);
9698

9799
// the spacings between currency symbol and number are ignored, because
@@ -103,14 +105,14 @@ private static function getPattern($currency)
103105
preg_match('/^([^\s\xc2\xa0]*)[\s\xc2\xa0]*123(?:[,.]0+)?[\s\xc2\xa0]*([^\s\xc2\xa0]*)$/u', $pattern, $matches);
104106

105107
if (!empty($matches[1])) {
106-
self::$patterns[\Locale::getDefault()] = $matches[1].' {{ widget }}';
108+
self::$patterns[$locale][$currency] = $matches[1].' {{ widget }}';
107109
} elseif (!empty($matches[2])) {
108-
self::$patterns[\Locale::getDefault()] = '{{ widget }} '.$matches[2];
110+
self::$patterns[$locale][$currency] = '{{ widget }} '.$matches[2];
109111
} else {
110-
self::$patterns[\Locale::getDefault()] = '{{ widget }}';
112+
self::$patterns[$locale][$currency] = '{{ widget }}';
111113
}
112114
}
113115

114-
return self::$patterns[\Locale::getDefault()];
116+
return self::$patterns[$locale][$currency];
115117
}
116118
}

‎tests/Symfony/Tests/Component/Form/Extension/Core/Type/MoneyTypeTest.php

Copy file name to clipboardExpand all lines: tests/Symfony/Tests/Component/Form/Extension/Core/Type/MoneyTypeTest.php
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,18 @@ public function testMoneyPatternWorksForYen()
3333
$view = $form->createView();
3434
$this->assertTrue((Boolean) strstr($view->get('money_pattern'), '¥'));
3535
}
36+
37+
// https://github.com/symfony/symfony/issues/5458
38+
public function testPassDifferentPatternsForDifferentCurrencies()
39+
{
40+
\Locale::setDefault('de_DE');
41+
42+
$form1 = $this->factory->create('money', null, array('currency' => 'GBP'));
43+
$form2 = $this->factory->create('money', null, array('currency' => 'EUR'));
44+
$view1 = $form1->createView();
45+
$view2 = $form2->createView();
46+
47+
$this->assertSame('{{ widget }} £', $view1->get('money_pattern'));
48+
$this->assertSame('{{ widget }} €', $view2->get('money_pattern'));
49+
}
3650
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.