【问题标题】:How to set a decimal in symfony2如何在 symfony2 中设置小数点
【发布时间】:2017-04-17 17:20:52
【问题描述】:

我一直试图简单地将字段类型设置为 0 到 1 之间的小数,但是无论我的配置如何,原则总是将我的小数从我的表单字段四舍五入到最接近的整数。

我的实体:

/**
 * @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 类型。需要改成number类型。

标签: php symfony doctrine-orm fieldtype


【解决方案1】:

最后,在 Symfony 4 中,您需要将选项 HTML5 设置为 true,如果不设置,则字段为文本类型...。 'html5' => true

【讨论】:

    【解决方案2】:

    整数不是小数。使用像 ehymel 提到的数字:

    ->add('nuetu', NumberType::class, array(
        'scale' => 1,
        'attr' => array(
            'min' => 0,
            'max' => 1,
            'step' => '.1',
        ),
        'label' => 'Lef a nuetu',
        'required' => false,
    ))
    

    以上应该可以工作。

    【讨论】:

      猜你喜欢
      • 2014-06-03
      • 2012-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多