Search code examples
phpdoctrinesymfonyformbuilder

Selected option not set in EntityType::class formfield symfony3


I'm very new at working with a framework like symfony, but I must say that i am turning in to a fan quite fast. Unfortunately i'm totally stuck for the last few days in a row.

The Context
I'm trying to refactor my old-school php CMS to the symfony3 framework. A user can manage his pages by adding en editing them. Adding a new page to the database works like a charm. Editing a page is also working fine, except one small part of it. The form prefills al fields like it should and a post will edit the entity.

But... for some reason the selectbox won't pre-select with the selected templatetype. This list is build with use of the EntityType::class and fetches the available data from the database by using AppBundle:Templates.

A piece of the code i use for loading and building the form:

    // Fetch selected page
    $page = $this->getDoctrine()->getRepository('AppBundle:PageMan')->find($id);        

    // Generate form
    $form = $this->createFormBuilder($page)
      ->add('templateId', EntityType::class, array(
        'label' => 'Template type',
        'class' => 'AppBundle:Templates',
        'placeholder' => 'Kies een template',
        'choice_label' => 'name',
        'choice_value' => 'id',
        'multiple' => false,
        'expanded' => false,
        'required' => true,
       ))->getForm();

The last few days i have tried every possibility i could think of and google. I've also checked the following:

  • templateId is filled with a value after loading the repository. When I change the field to a plain textfield, the value is shown.
  • AppBundle:Templates returns unique values (only two entity's in the database with id 1 and 2)
  • The selected="selected" attribute is set when posting (and not redirecting away)
  • Removing cashe doesn't solve the problem

At his moment i'm out of possible solutions, hope some one can help. It could be something so simple, but i'm just not seeing it anymore.

--Update--
Just found the following inconsistency in a vardump of the generated form. In the 'name'-object you see modelData, normData and viewData pre-filled. But 'templateId' misses content in viewData.

screenshot vardump formbuilder

In the documentation of symfony it states the folowing:

View Data - This is the format that's used to fill in the form fields themselves. It's also the format in which the user will submit the data. When you call Form::submit($data), the $data is in the "view" data format. Source

This could be a possible leed to a solution.

--Update 2--
Just hardcoded $this->viewData in \vendor\symfony\symfony\src\Symfony\Component\Form\Form.php to a hardcoded value that is present in the selectbox. This adds a value to the empty setting as mentioned in the the update above. Now the default value gets selected as it should. I'm going to follow this variable in the code. Hope to find the reason why it doesnt get prefilled.


Solution

  • Finally fixed my problem. Don't know if it's the official way, but in my case it works like a charm.

    In my EntityType setting, i'm now passing 'data' => $page, as an extra object to the formbuilder. This results in viewData being filled with a value and that was what i needed to get a pre-selectid selectbox on pageload.

    Here the final snippet:

                ->add('templateId', EntityType::class, array(
                'label' => 'Template type',
                'class' => TemplateMan::class,
                'placeholder' => 'Kies een template',
                'choice_label' => 'name',
                'choice_value' => 'templateId',
                'data' => $page,
                'data_class' => null,
                'multiple' => false,
                'expanded' => false,
                'required' => true,
            ))