Description
I have a basic form that has two fields: one a normal text field, and one an entity field defined like this:
$builder->add('division', 'entity', [
'class' => 'MyApp:Division',
'property' => 'division',
'required' => false
]);
This renders the field properly on the screen: it has an empty <option>
tag and then the rest of the MyApp:Division
entities loaded.
If I submit the form through a GET request and handle it like this:
if ($request->query->has($searchForm->getName())) {
$searchForm->submit(
$request->query->get($searchForm->getName())
);
}
I get a Doctrine exception thrown saying it can not execute a query with an empty IN()
clause (truncated for clarity):
An exception occurred while executing 'SELECT ... WHERE d0_.id IN (?)' with params [""]: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "" /var/apps/my-app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php 91
Curiously, if I set the choices
key in the form options, an empty submission works:
// Assume $this->divisions was passed in through the constructor.
$builder->add('division', 'entity', [
'class' => 'MyApp:Division',
'choices' => $this->divisions
'property' => 'division',
'required' => false
]);
I've narrowed down the offending code to the Symfony\Bridge\Doctrine\Form\ChoiceList\DoctrineChoiceLoader
class on line 143 in the loadChoicesForValues()
method.
The $values
array looks like this when the choices
key is absent and an empty field is submitted:
array (
0 => ''
)
Which causes the IN()
query to fail.