【问题标题】:Expected argument of type "string", "Doctrine\ORM\PersistentCollection" given给定“字符串”、“Doctrine\ORM\PersistentCollection”类型的预期参数
【发布时间】:2017-04-11 20:23:11
【问题描述】:

我将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”。

【问题讨论】:

    标签: symfony symfony-forms symfony-validator


    【解决方案1】:

    @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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      • 2019-01-30
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      • 2012-09-23
      • 1970-01-01
      相关资源
      最近更新 更多