dqouryz3595 2015-04-17 09:43
浏览 38
已采纳

Symfony2 LexikFormFilterBundle:filter_entity的空值导致表单错误

I'm using the LexikFormFilterBundle (current dev-version) with Symfony 2.7.0-BETA1. It works as expected, but…

The Problem

all filter_entity fields throw form errors, when the empty option is submitted (i.e. no entity is selected). It is documented in the basic example, that one has to set

'validation_groups' => array('filtering') // avoid NotBlank() constraint-related message

But in my case, the ManyToOne Entities don't use a NotBlank()-Assert. Despite this, the form throws errors about non existant entities ''. When I select an entity, the filter filters correctly and no error message appears. Other filter field types also don't throw errors, when they are empty.

Here is a stripped down representation of my code:

Entity code:

<?php
// src/AppBundle/Entity/Serviceevent.php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Serviceevent
 *
 * @ORM\Table(name="serviceevents")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\ServiceeventRepository")
 */
class Serviceevent
{
    /**
     * @var \AppBundle\Entity\Park
     *
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Park", inversedBy="serviceevents")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="park_id", referencedColumnName="id", nullable=false)
     * })
     */
    private $park;
}

Form filter code:

<?php
// /src/AppBundle/Filter/ServiceeventFilterType.php
namespace AppBundle\Filter;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ServiceeventFilterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('park', 'filter_entity', array(
                'class' => 'AppBundle:Park',
                'property' => 'identifyingName',
                'label' => 'Park',
                'placeholder' => '',
                'required' => false,
            ));
        ;
    }

    public function getName()
    {
        return 'serviceevent_filter';
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'csrf_protection'   => false,
            'validation_groups' => array('filtering'), // avoid NotBlank() constraint-related message
        ));
    }
}

What I expect

I expect, that with no value selected, no filtering for that field happend without an error thrown.

What I get

Instead, I get form errors for all entity-fields like this one:

Symfony\Component\Validator\ConstraintViolation Object(Symfony\Component\Form\Form).children[park] =

Caused by:

Symfony\Component\Form\Exception\TransformationFailedException Unable to reverse value for property path "[park]": The choice "" does not exist or is not unique

Caused by:

Symfony\Component\Form\Exception\TransformationFailedException The choice "" does not exist or is not unique

Question is

The question is: how do I get rid of those errors?

  • 写回答

1条回答 默认 最新

  • dougu1990 2015-04-22 15:30
    关注

    User wcluijt's fix in https://github.com/symfony/symfony/issues/14393#issuecomment-94996862 fixed this for me. This confirms, that my problem here was provoked by a Bug in Symfony 2.7.0-BETA1. So wo can close this question as fixed. Sorry for wasting your time with a beta-related bug.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?