如何在symfony 2中创建一个以逗号或句点接受数字的数字字段 [英] How to create a number field that accepts numbers with comma or period in symfony 2
问题描述
我有一个带有十进制字段的应用程序,例如:
I have an application with a decimal field like:
/**
* @var decimal $amount
*
* @ORM\Column(name="amount", type="decimal", scale="2")
*/
private $amount;
我希望该表单接受格式为 3,4或 3.4的数字
I want that the form to accept numbers with a format like "3,4" or "3.4".
如果我输入 3.4,应用程序将保存在数据库 3.4中;如果我输入 3,4,则应用程序将保存在数据库 34中(是的,没有逗号并且没有显示验证错误!)。
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!).
(这是一个已知的symfony错误: https://github.com/symfony/symfony/issues/2059 )
(This is a known symfony bug: https://github.com/symfony/symfony/issues/2059 )
因此,我该如何接受带逗号和小数点的数字?
(我已经尝试在DataTrasformer中用逗号代替逗号,但是DataTransformer需要已经标准化的数字。)
(I already tried to substitute commas with dot in a DataTrasformer, but DataTransformer takes the number already normalized.)
推荐答案
我找到了将DataTransformer与appendClientTransformer结合使用的解决方法,以下是代码段: https://gist.github.com/3394880
I found a workaround using a DataTransformer with appendClientTransformer, here is the snippet: https://gist.github.com/3394880
这篇关于如何在symfony 2中创建一个以逗号或句点接受数字的数字字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!