我想在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
))
有人可以帮忙吗?
答案 0 :(得分:0)
对于Symonfy3,尝试这个yaml验证:
AppBundle\Entity\SomeEntity:
properties:
price:
- NotBlank: ~
- GreaterThanOrEqual:
value: 0
NotBlank: ~
需要该字段。