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

和自甴很熟 提交于 2020-01-03 13:07:33

问题


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.



来源:https://stackoverflow.com/questions/12026050/how-to-create-a-number-field-that-accepts-numbers-with-comma-or-period-in-symfon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!