Symfony2 choice field : empty value

I’m stumbling upon a really simple question and I can’t find out what I’m doing wrong :

I have an entity Post which can have a type, in my class declaration :

 * @ORMColumn(name="type", type="text", nullable=true)
private $type;

Then I want a form to create Posts :
In my PostType::buildForm() function :

$builder->add('type', 'choice', array(
        'empty_data' => null,
        'empty_value' => 'No type',
        'multiple' => false,
        'expanded' => true,
        'choices' => MyBundleEntityApplicationPost::getTypes(), /* returns array('TYPE1' => 'TYPE_1', 'TYPE2' => 'TYPE_2', ...) */
        'required' => true,))

The plan is to have a radio list with :

  • Type 1
  • Type 2
  • Type 3
  • No type

But it seems like if I choose the option ‘No type’, the form won’t validate, without giving any explicit error. Same thing happends with ‘required’ => true, with ‘placeholder’ instead of ’empty_value’, …

Can you spot my mistake ?
What am I doing wrong ?

Thanks 🙂

Source: forms

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.