-
-
Save FabianSchmick/eebcd59d36eedf1829eb504e1c0c4c49 to your computer and use it in GitHub Desktop.
Symfony DataTransformer for decimals (number) with comma and decimal points
This file contains hidden or 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 AppBundle\Form; | |
use AppBundle\Form\DataTransformer\NumberToStringTransformer; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\Extension\Core\Type\TextType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
/** | |
* Reusable DecimalType Field with usage of NumberToStringTransformer | |
* | |
* @see http://symfony.com/doc/3.4/form/data_transformers.html#creating-a-reusable-issue-selector-field | |
*/ | |
class DecimalType extends AbstractType | |
{ | |
/** | |
* @var NumberToStringTransformer | |
*/ | |
private $numberToStringTransformer; | |
public function __construct(NumberToStringTransformer $numberToStringTransformer) | |
{ | |
$this->numberToStringTransformer = $numberToStringTransformer; | |
} | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->addModelTransformer($this->numberToStringTransformer); | |
} | |
public function getParent() | |
{ | |
return TextType::class; | |
} | |
} |
This file contains hidden or 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 AppBundle\Form\DataTransformer; | |
use Symfony\Component\Form\DataTransformerInterface; | |
use Symfony\Component\Form\Exception\UnexpectedTypeException; | |
use Symfony\Component\Form\Exception\TransformationFailedException; | |
/** | |
* A simple number transformer that allows either comma or dot as decimal separator. | |
* | |
* Caution: this transformer does not understand thousands separators. | |
* | |
* @see http://stackoverflow.com/questions/12026050/how-to-create-a-number-field-that-accepts-numbers-with-comma-or-period-in-symfon | |
* @see https://gist.github.com/chrif/3411067 | |
*/ | |
class NumberToStringTransformer implements DataTransformerInterface | |
{ | |
/** | |
* @param mixed $value | |
* @return string | |
*/ | |
public function transform($value) | |
{ | |
if ($value === null) { | |
return ''; | |
} | |
if (!is_numeric($value)) { | |
throw new UnexpectedTypeException($value, 'numeric'); | |
} | |
$value = str_replace('.', ',', $value); | |
return (string)$value; | |
} | |
/** | |
* @param mixed $value | |
* @return float|mixed|null | |
*/ | |
public function reverseTransform($value) | |
{ | |
if (!$value || $value === '') { | |
return null; | |
} | |
if (!is_string($value)) { | |
throw new UnexpectedTypeException($value, 'string'); | |
} | |
$value = str_replace(',', '.', $value); | |
if (is_numeric($value)) { | |
$value = (float)$value; | |
} else { | |
throw new TransformationFailedException('not a number'); | |
} | |
return $value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment