Symfony2:防止货币领域的投入是负面的

时间:2021-04-29 20:15:38

I want to prevent negative values in a Symfony2 Formbuilder money field, but adding a Min(0) or Range(min=0) assert to the annotation of the attribute in the entity doesn't work. The attributes in the entity looks like this:

我想在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 looks like this:

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
            ))

Can anybody help pls?

有人可以帮忙吗?

1 个解决方案

#1


0  

For Symonfy3 try this yaml validation:

对于Symonfy3,请尝试以下yaml验证:

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

NotBlank: ~ makes the field required.

NotBlank:〜使字段成为必需。

#1


0  

For Symonfy3 try this yaml validation:

对于Symonfy3,请尝试以下yaml验证:

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

NotBlank: ~ makes the field required.

NotBlank:〜使字段成为必需。