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?