Pārlūkot izejas kodu

[FormType] Fixed broken MoneyType regexp for JPY

The regexp in MoneyType doesn't work if currency format has no decimal
(like JPY) and doesn't work either if the currency symbol is unicode

This change fixes both issues and adds a unit test
Manuel Kiessling 13 gadi atpakaļ
vecāks
revīzija
e814d273c2

+ 1 - 1
src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php

@@ -100,7 +100,7 @@ class MoneyType extends AbstractType
 
 
             // the regex also considers non-break spaces (0xC2 or 0xA0 in UTF-8)
             // the regex also considers non-break spaces (0xC2 or 0xA0 in UTF-8)
 
 
-            preg_match('/^([^\s\xc2\xa0]*)[\s\xc2\xa0]*123[,.]00[\s\xc2\xa0]*([^\s\xc2\xa0]*)$/', $pattern, $matches);
+            preg_match('/^([^\s\xc2\xa0]*)[\s\xc2\xa0]*123(?:[,.]0+)?[\s\xc2\xa0]*([^\s\xc2\xa0]*)$/u', $pattern, $matches);
 
 
             if (!empty($matches[1])) {
             if (!empty($matches[1])) {
                 self::$patterns[\Locale::getDefault()] = $matches[1].' {{ widget }}';
                 self::$patterns[\Locale::getDefault()] = $matches[1].' {{ widget }}';

+ 12 - 0
tests/Symfony/Tests/Component/Form/Extension/Core/Type/MoneyTypeTest.php

@@ -11,7 +11,10 @@
 
 
 namespace Symfony\Tests\Component\Form\Extension\Core\Type;
 namespace Symfony\Tests\Component\Form\Extension\Core\Type;
 
 
+use Symfony\Component\Form\FormFactory;
+
 require_once __DIR__ . '/LocalizedTestCase.php';
 require_once __DIR__ . '/LocalizedTestCase.php';
+require_once __DIR__ . '/../../../../../../../../src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php';
 
 
 class MoneyTypeTest extends LocalizedTestCase
 class MoneyTypeTest extends LocalizedTestCase
 {
 {
@@ -24,4 +27,13 @@ class MoneyTypeTest extends LocalizedTestCase
 
 
         $this->assertSame('{{ widget }} €', $view->get('money_pattern'));
         $this->assertSame('{{ widget }} €', $view->get('money_pattern'));
     }
     }
+
+    public function testMoneyPatternWorksForYen()
+    {
+        \Locale::setDefault('en_US');
+
+        $form = $this->factory->create('money', null, array('currency' => 'JPY'));
+        $view = $form->createView();
+        $this->assertTrue((bool)strstr($view->get('money_pattern'), '¥'));
+    }
 }
 }