How to create a numeric field that accepts comma or period numbers in symfony 2

I have an application with a decimal field, for example:

/** * @var decimal $amount * * @ORM\Column(name="amount", type="decimal", scale="2") */ private $amount; 

I want the form to accept numbers in the format "3.4" or "3.4".

If I enter “3.4”, the application saves in the database “3.4”, if I enter “3.4”, the application saves in the database “34” (yes, without a comma and without checking the validation error!).

(This is a known symfony bug: https://github.com/symfony/symfony/issues/2059 )

So, how can I accept numbers with commas as well as decimal points?

(I already tried to replace commas with a period in a DataTrasformer, but the DataTransformer accepts a number that is already normalized.)

+4
source share
2 answers

I found a workaround using DataTransformer with appendClientTransformer, here is a snippet: https://gist.github.com/3394880

+5
source

I had the same problem, and I decided to create my own field with a number without language formatting. This is the transformer I came across: https://gist.github.com/3411067

Note: I needed to make a TransformationFailedException in order to get the validation valid.

+2
source

All Articles