我一直试图将字段类型设置为0到1之间的小数,但无论我的配置如何,doctrine总是将我的小数从我的表单字段舍入到最接近的整数。
我的实体:
/**
* @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,
))
My Twig:
{{ 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
答案 0 :(得分:1)
最后,在Symfony 4中,您需要将HTML5选项设置为true,否则,将字段设置为文本类型。
'html5' => true
答案 1 :(得分:0)
整数不是小数。使用像ehymel提到的数字:
->add('nuetu', NumberType::class, array(
'scale' => 1,
'attr' => array(
'min' => 0,
'max' => 1,
'step' => '.1',
),
'label' => 'Lef a nuetu',
'required' => false,
))
上述情况应该有效。