如何在symfony2中设置一个小数
问题描述:
我一直试图简单地设置一个字段类型为0到1之间的一个小数,但是无论我有什么配置,doctrine总是将我的小数位从我的表单字段舍入到最近的整数。如何在symfony2中设置一个小数
我的实体:
/**
* @var integer
*
* @Assert\Range(
* min=0,
* max=1,
* minMessage = "this must be greater than or equal to 0.",
* maxMessage = "this must be less than or equal to 1."
*)
* @ORM\Column(name="nuetu", type="decimal", precision=2, scale=1, nullable=true)
*/
private $nuetu;
我的字段类型:
->add('nuetu', 'integer', array(
'scale' => 1,
'attr' => array(
'min' => 0,
'max' => 1,
'step' => '.1',
),
'label' => 'Lef a nuetu',
'required' => false,
))
我的枝杈:
{{ form_row(form.nuetu, {'type': 'number'}) }}
我也试过{{ form_row(form.nuetu) }}
&不使用assert
,并没有宣布precision
或scale
在我的实体注释中。
我的目标是将数字保持为(0.3或0.8)。
我看了这些问题,但我一直不成功:
What is the right way to define annotation for DECIMAL type in Doctrine2
Rounded decimal in edit form Symfony2 + Doctrine2
http://symfony.com/doc/2.7/reference/forms/types/integer.html
http://symfony.com/doc/2.7/reference/forms/types/number.html#scale
答
Integer是不是一个小数。使用像ehymel提到的数字:
->add('nuetu', NumberType::class, array(
'scale' => 1,
'attr' => array(
'min' => 0,
'max' => 1,
'step' => '.1',
),
'label' => 'Lef a nuetu',
'required' => false,
))
上面应该工作。
您的字段类型指定一个'整数'类型。您需要更改为'号码'类型。 – ehymel