0

I just need to add other defaults options to an "entity field" in Symfony2. It displays names, and I need an option "Name not in list", but I cant find a way to achieve it. Data transformers cant seem to fix my problem though.

$builder
      ->add('family', 'entity', array(
            'class'     => 'InterneFichierBundle:Family',
            'property'  => 'Name'
        ))

If the name of the family is not in the list, there should be an option "name not in list".. Thanks a lot !

2
  • did you have some code to explain it better ? Commented May 6, 2014 at 9:13
  • just edited the question Commented May 6, 2014 at 9:16

2 Answers 2

1

i'm pretty sure you can just specify an empty value option:

$builder
  ->add('family', 'entity', array(
        'class'     => 'InterneFichierBundle:Family',
        'property'  => 'Name',
        'empty_value' => 'Name not in list',
    ))

see http://symfony.com/doc/current/reference/forms/types/entity.html#empty-value

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I could use an empty value, but I can only use one, is there any generic solution to add more options ?
you'd basically need to drop down a level from then entity type to the choice field type. You'd need to build your choices array yourself and then used a data transformer to have certain values stay as null. Your other option is to add in your options as rows in your db and use a flag of some sort to show that they are not "real" options, and then handle the changes yourself.
0

You shouk try with :

for information you can see it here : http://symfony.com/fr/doc/current/reference/forms/types/entity.html

    $builder->add('users', 'entity', array(
      'class' => 'AcmeHelloBundle:User',
      'query_builder' => function(EntityRepository $er) {
         return $er->createQueryBuilder('u')
             ->orderBy('u.username', 'ASC');
       },
     ));

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.