Symfony2嵌入表单:错误未显示

我有订单和产品实体

class Order {

    /**
     * @ORM\OneToMany(targetEntity="Product", mappedBy="order", cascade={"persist", "remove"}))
     * @Assert\Valid()
     * @Assert\Count(
     *      min = "1"
     * )
     */
     protected $products;
}

class Product
{
    /**
     * @ORM\ManyToOne(targetEntity="Order", inversedBy="products")
     * @ORM\JoinColumn(name="id_order", referencedColumnName="id", onDelete="CASCADE", nullable=false)
     */
    protected $order;

    /**
     * @ORM\ManyToOne(targetEntity="Category")
     * @ORM\JoinColumn(name="id_category", referencedColumnName="id", nullable=false)
     */
    protected $category;

    /**
     * @ORM\ManyToOne(targetEntity="Size")
     * @ORM\JoinColumn(name="id_size", referencedColumnName="id", nullable=true)
     */
    protected $size;


    /**
     * @Assert\IsTrue(message = "Please enter a size")
     */
    public function isSizeValid()
    {
        if($this->category->getCode() == 1 && is_null($this->size)) {
            return false;
        }
        return true;
    }

}

在我的orderType表单中,我添加了一个ProductTypes集合,其中error_bubbling设置为false.但是当我的isSizeValid为false时,我的表单上没有任何错误消息.

但是当我将error_bubbling设置为true时,错误会显示在我的表单顶部.

如何在twig模板中显示每个产品旁边的错误?

最佳答案 您必须在表单中设置“error_mapping”选项.

http://symfony.com/doc/current/reference/forms/types/form.html#error-mapping

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'error_mapping' => array(
            'isSizeValid' => 'product',
        ),
    ));
}
点赞