How to create a number field that accepts numbers

2019-06-22 17:47发布

问题:

I have an application with a decimal field like:

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

I want that the form to accept numbers with a format like "3,4" or "3.4".

If I enter "3.4" the application save in the database "3.4", if I enter "3,4" the application saves in the database "34" (yes, without comma and without showing 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 substitute commas with dot in a DataTrasformer, but DataTransformer takes the number already normalized.)

回答1:

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



回答2:

I had the same problem and I decided to make my own number field without the locale formatter. This is the transformer I came up with: https://gist.github.com/3411067

Note: one thing I had to do is to throw a TransformationFailedException to get the validation right.