1

我想防止在 Symfony2 Formbuilder 货币字段中出现负值,但是在实体中属性的注释中添加 Min(0) 或 Range(min=0) 断言不起作用。实体中的属性如下所示:

    /**
 * @ORM\Column(name="price", type="integer")
 * @Assert\Type(type="integer", message="The value {{ value }} is not a valid {{ type }}.")
 * @Assert\Range( min = 0)
 * @var int
 */
private $price;

/**
 * @ORM\Column(name="setupFee", type="integer")
 * @Assert\Type(type="integer", message="The value {{ value }} is not a valid {{ type }}.")
 * @Assert\Range( min = 0)
 * @var int
 */
private $setupFee;

FormType 看起来像这样:

    $builder->add('address', null, array('label' => 'Kunde:'))          
            ->add('productGroup', 'entity', array(
                'label'     => 'Produktkategorie:',
                'class'     => 'Ttm\AccountingBundle\Entity\ProductGroup',
                'property'  => 'name'
            ))
            ->add('dedicatedServer', 'entity', array(
                'label'     => 'Server:',
                'class'     => 'Ttm\ServerBundle\Entity\DedicatedServer',
                'property'  => 'labelname',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('dedicatedServer')
                        ->where('dedicatedServer.contract IS NULL');
                },
                'empty_data'    => null,
                'required'      => false,
            ))
            ->add('productDetails', 'textarea', array(
                    'label' => 'Produktdetails:',
                    'attr' => array('style' => 'width:350px',
                    'rows' => 4,)
            ))
            ->add('price', 'money', array(
                'label'     => 'Nettopreis:',
                'divisor'   => 100
            ))
            ->add('setupFee', 'money', array(
                'label'     => 'Einrichtungsgebühr:',
                'divisor'   => 100
            ))

有人可以帮忙吗?

4

1 回答 1

0

对于 Symonfy3 试试这个 yaml 验证:

AppBundle\Entity\SomeEntity:
    properties:
        price:
            - NotBlank: ~
            - GreaterThanOrEqual:
                value: 0

NotBlank: ~使该字段成为必需的。

于 2016-06-05T08:09:34.600 回答