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.

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

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名