1

In a Symfony form I have one field called slot that matches an entity with a custom query. let's say something like this:

->add('slot', 'entity', array(
    'label'             => 'Slot',
    'class'             => 'FooBarBundle:Slot',
    'property'          => 'name',
    'required'          => false,
    'query_builder'     => function(\Foo\BarBundle\Entity\SlotRepository $er) use ($ids) {
        return $er->createQueryBuilder('u')
            ->where('u.id IN(:ids)')->setParameter('ids', $ids);
    }
))

so far every thing works. The slot is a ManyToOne matching on the Team entity (for which the form is), so there can only be one slot selected at a time. As I said, every thing going according to plan. The user can select a slot and save it and every one's happy.

The trouble starts when you try to "unselect" a slot (yep that has to be possible). So I need to add a none value with a caption like None of the listed items or what ever. The question is how can I do that?

4
  • 3
    Maybe 'empty_data' => null, 'empty_value' => null ? Commented Aug 10, 2015 at 3:05
  • 2
    As @absalon.valdes says, 'empty_value' => 'None of the listed items' would do the job. Commented Aug 10, 2015 at 7:15
  • Sounds exactly like what I was looking for. I'll check it as soon as I get home. Could you might make an answer out of your comment, so I can accept it, once I tested it? Commented Aug 10, 2015 at 8:58
  • 1
    @absalon.valdes could you make an answer out of your comment? that was exactly, what I was looking for! Commented Aug 10, 2015 at 15:00

2 Answers 2

1

You should use empty_data and empty_value:

->add('slot', 'entity', array(
    'label'             => 'Slot',
    'class'             => 'FooBarBundle:Slot',
    'property'          => 'name',
    'required'          => false,
    'query_builder'     => function(\Foo\BarBundle\Entity\SlotRepository $er) use ($ids) {
        return $er->createQueryBuilder('u')
            ->where('u.id IN(:ids)')->setParameter('ids', $ids);
    },

    'empty_data'  => null,
    'empty_value' => 'None of the listed items' /* or null */
))
Sign up to request clarification or add additional context in comments.

Comments

0

The way that choice fields (which the entity field extends) are rendered within Symfony forms is defined by a certain kind of logic (documentation). I'm guessing that you're rendering a <select> dropdown, in which case if you're using Symfony 2.6 or above, placeholder is what you want (documentation). Be warned though, with certain databases (e.g. Postgres), the placeholder option was broken in Symfony 2.7.0 and (I think) 2.7.1.

The empty_data / empty_value issue with forms is part of a longstanding confusion. In short, use placeholder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.