[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
This commit is contained in:
Manuel Kiessling 2012-01-17 18:33:18 +01:00
parent 4ae41a56c4
commit e814d273c2
2 changed files with 13 additions and 1 deletions

View File

@ -100,7 +100,7 @@ class MoneyType extends AbstractType
// 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])) {
self::$patterns[\Locale::getDefault()] = $matches[1].' {{ widget }}';

View File

@ -11,7 +11,10 @@
namespace Symfony\Tests\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\FormFactory;
require_once __DIR__ . '/LocalizedTestCase.php';
require_once __DIR__ . '/../../../../../../../../src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php';
class MoneyTypeTest extends LocalizedTestCase
{
@ -24,4 +27,13 @@ class MoneyTypeTest extends LocalizedTestCase
$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'), '¥'));
}
}