Andrei Herford
Andrei Herford

Reputation: 18729

Why does Symfony 3.4 form displays EntityType choices with label although 'label' => false was used?

I am working on an existing Symfony 3.4 based project and trying to add and render a new form. Although the 'label' => false option was used, the fields are rendered including a label. Why?

// Symfony
class SomeController extends Controller {        
    public function userListAction(Request $request) {  
        $users = $someService->getUsers();

        $formBuilder = $this->createFormBuilder()
            ->add('users', EntityType::class, [
                'label' => false,                  // also tested '' and 'someLabel'
                'class' => 'AppBundle:User',
                'choices' => $users,
                'multiple' => true,
                'expanded' => true,
            ]);

        $variables = array(
            'form' => $formBuilder->getForm()->createView(),
        );

        return $this->render('AppBundle:Pages:user_list.html.twig', $variables);
    }
}


// Twig
{% extends 'AppBundle::layout.html.twig' %}

{% block page_content %}
    {{ form_start(form) }}
    {{ form_widget(form) }}
    {{ form_end(form) }}
{% endblock %}

This shows a list of checkboxes for all users including the username as label.

Where does Symfony get the information to use the username as label? As far as I know no custom form widget was defined for the User class. Is there any way to check this for sure? Maybe there is something hidden in the vendor bundles like FOSUserBundle?

Why is the 'label' => false option ignored?


Edit:

Different ways of rendering the form does not solve the problem:

{{ form_start(form) }}
{{ form_row(form) }}
{{ form_end(form) }}

Result:
<div id="form_users">
     <div class="form-group">
         <div class="checkbox">
             <label for="form_users_547">
                 <input type="checkbox" id="form_users_547" name="form[users][]" value="547"> 
                     someUserName
             </label>
         </div>
      </div>
 </div>



{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

Result:
<div id="form_users">
         <div class="checkbox">
             <label for="form_users_547">
                 <input type="checkbox" id="form_users_547" name="form[users][]" value="547"> 
                     someUserName
             </label>
         </div>
 </div>



{{ form_start(form) }}
    {% for userFormView in form.users %}
         {{ form_row(userFormView) }}
    {% endfor %}
{{ form_end(form) }}

Result:
Basically the same as before with form_row

Upvotes: 0

Views: 563

Answers (2)

Alexander Dimitrov
Alexander Dimitrov

Reputation: 974

You need to use ‘choice_label’ => ‘YOUR PROPERTY PATH’ in the field options.

Pretty match is written in the docs: https://symfony.com/doc/current/reference/forms/types/entity.html#choice-label

If the entity class cast to string then is used if is not it will throw an exception. It looks like your entity User cast to the user name and that’s why it works.

Upvotes: 2

Alessandro Lai
Alessandro Lai

Reputation: 2274

You should try to use {{ form_row(form) }} which should render the whole field correctly.

Upvotes: 0

Related Questions