0

我正在使用collectiontype中的票证字段formbuilder并尝试为其添加服务器端验证。但是只有在添加断言验证时我才会遇到一些错误。

我的实体:

/**
* @Assert\Length(
* min = 1,
* max = 10,
* minMessage = "Atlest one ticket to be added",
* maxMessage = "Not allowed"
* )
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\EventTicket", inversedBy="events", cascade={"persist"})
*/
public $tickets;

我的表格:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('tickets', CollectionType::class, [
            'entry_type' => EventTicketType::class,
            'allow_add' => true,
            'allow_delete' => true
        ]
    );
}

我收到此错误:

给定“字符串”类型的预期参数,“Doctrine\ORM\PersistentCollection”。

4

1 回答 1

3

@Assert\Length是一个字符串约束,不能用于集合类型。您需要@Assert\Count用于集合类型。它应该是这样的:

/**
 * @Assert\Count(
 *      min = 1,
 *      max = 10,
 *      minMessage = "At least one ticket to be added",
 *      maxMessage = "Not allowed"
 * )
  * 
 * @ORM\ManyToMany(targetEntity="AppBundle\Entity\EventTicket", inversedBy="events", cascade={"persist"})
 */
public $tickets;
于 2016-11-28T07:38:00.220 回答