Created
September 26, 2012 21:37
-
-
Save charlieman/3790749 to your computer and use it in GitHub Desktop.
NumberType override to use 'en' locale in the NumberFormatter to allow for dot as the decimal separator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Redeye\CuyBundle\Form\Extension\Core\DataTransformer; | |
use Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer as BaseTransformer; | |
class NumberToLocalizedStringTransformer extends BaseTransformer | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function getNumberFormatter() | |
{ | |
$formatter = new \NumberFormatter('en', \NumberFormatter::DECIMAL); | |
if ($this->precision !== null) { | |
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->precision); | |
$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode); | |
} | |
$formatter->setAttribute(\NumberFormatter::GROUPING_USED, $this->grouping); | |
return $formatter; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Redeye\CuyBundle\Form\Extension\Core\Type; | |
use Symfony\Component\Form\Extension\Core\Type\NumberType as BaseNumberType; | |
use Symfony\Component\Form\FormBuilder; | |
use Redeye\CuyBundle\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer; | |
class NumberType extends BaseNumberType | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(FormBuilder $builder, array $options) | |
{ | |
$builder->appendClientTransformer(new NumberToLocalizedStringTransformer( | |
$options['precision'], | |
$options['grouping'], | |
$options['rounding_mode'] | |
)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
form.type.number: | |
class: Redeye\CuyBundle\Form\Extension\Core\Type\NumberType | |
tags: | |
- { name: form.type, alias: number } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment