2

My problem is as follows.

I have a drop down list in form builder which successfully retrieves the data from the database.

 public function buildForm(FormBuilder $builder, array $options) {
            $builder->add('Statusname', 'entity', array('empty_value' => 'All','class' => 'MyProject\EntityBundle\Entity\IssueStatusType', 'property' => 'name', 'required' => false,'query_builder' => function ($repository) { return $repository->createQueryBuilder('es')->orderBy('es.name', 'ASC'); },)) 
}

It works fine. But when I add my custom data

'not closed' => 'Not closed'

into the drop down list i.e

public function buildForm(FormBuilder $builder, array $options) {
            $builder->add('Statusname', 'entity', array('empty_value' => 'All','not closed' => 'Not closed','class' => 'MyProject\EntityBundle\Entity\IssueStatusType', 'property' => 'name', 'required' => false,'query_builder' => function ($repository) { return $repository->createQueryBuilder('es')->orderBy('es.name', 'ASC'); },)) 
}

it does not work. can some one tell me why?

Thanks in advance.

5
  • If you use an entity field type you canno't add options like that, just add your option 'not closed' in your table IssueStatusType and it'll be show in your select box. More information about the entity field : [symfony.com/doc/current/reference/forms/types/entity.html] Commented Apr 25, 2012 at 10:06
  • I cannot add 'not closed' as the table IssueStatusType will be used in many places in my project in which option 'not closed' must not be displayed. Commented Apr 25, 2012 at 10:23
  • So you can try to make a choice field with the options of your table like in this post : stackoverflow.com/a/9491847/1259367 and just add your option 'not closed' in the choices. But i'm not sure this will work if you've a relation on this field. Commented Apr 25, 2012 at 11:36
  • I don't think it's a good idea to add non-existent entities to your choice list. You will run into problems if you try to persist a 'not_closed' entity. I think what you really want is to add a where condition to your query which filters out 'not closed' for those cases where 'not closed' is not a valid option. If you really want to do what you want then define your own form CustomEntityType. But again, I think you will run into problems. Commented Apr 25, 2012 at 13:48
  • @Cerad all depends on how data are used once they are bound. Commented Apr 25, 2012 at 17:10

2 Answers 2

5

The third parameters for FormBuilder::add() method is an asoociative array of options. 'not closed' is not a valid option so it does not work.

In your case you have to create your custom collection by hand and use the 'choice' type. In order to make it work you have to inject the entity manager to your form type. This is a minimalist example:

class IssueType extends AbstractType
{
    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('Statusname', 'choice', array(
            'empty_value' => 'All',
            'required'    => false,
            'choices'     => $this->buildStatusNames(),
        )
    }

    private function buildStatusNames()
    {
        $choices = array();
        $types = $this
            ->entityManager
            ->getRepository('MyProject\EntityBundle\Entity\IssueStatusType')
            ->createQueryBuilder('es')
            ->orderBy('es.name', 'ASC')
            ->getQuery()
            ->getResult();

        foreach ($types as $type) {
            // I assume key is retrieved by getId
            $choices[$type->getId()] = $type->getName();
        }

        $choices['not closed'] = 'Not closed';

        return $choices;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Have you actually tried this? I'm not so sure that this will work when dealing with relations. Ultimately you need objects and not just id's.
No, data from choices option are only used to build the form view, binding is done via a data transformer (which uses id). And you can not use objects as array keys.
One of us is very confused. Again, have you actually tried your method? Keep in mind that the goal is to establish relations between objects and not just to store id's. Have you looked at what EntityType does?
Again, the 'choice' option is not responsible of data transformation. Have you looked at what EntityChoiceList::getChoices() returns? Where in the question is it mentioned that this form aims to maintain relationship between objects? How could it be possible as a non-object choice is added? Again, relational mapping in EntityType is ensured by EntityToIdTransformer/EntitiesToArrayTransformer, not the choices option.
0

entity relationships are managed within the entity, here you are building a form for a view which will contain id's and readable names for your users.

When the form is submitted, grab the object using the id as JF Simon mentions above and submit, provided you have set everything up correctly in your entities, Symfony will take care of the rest.

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.