Rohit Patil
Rohit Patil

Reputation: 125

Label is not displaying in Symfony form

I am facing a issue to display label. Following is the code to generate form element.

$builder->add(
    'hearAboutUs', 'choice', [
        'choices' => ['Online Search'=>'Online Search',
            'Email'=> 'Email',
            'My Company' => 'My Company',
            'Colleague or Friend'=>'Colleague or Friend',
            'Existing Client' =>'Existing Client',
            'Direct Mail' => 'Direct Mail',
            'Other'=> 'Other',],
        'label' => 'How did you hear about us?',
        'required' => true,
        'expanded'=> true,
        'multiple' => false,
    ]
);

I am getting following output.enter image description here As like "Notes" label "How did you hear about us" label is not displaying.

Upvotes: 1

Views: 1273

Answers (3)

Rohit Patil
Rohit Patil

Reputation: 125

I have handled this using JQuery. On Form Load I am appending Label and for required I have handled this on the controller. But this is not proper solution, but by this way my problem solved.

Upvotes: 0

Goku
Goku

Reputation: 2139

You can try to override your form theme about choice widget.

You can do it like this :

1- Create a file named fields.html.twig in app/Resources/views/form/

2- In this file you have to extends default twig layout and override checkbox_widget adding <label> tag :

{% extends 'form_div_layout.html.twig' %}

{% block checkbox_widget %}
{% spaceless %}
    <label>
      <input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
    </label>
{% endspaceless %}
{% endblock checkbox_widget %}

3- Finally, tell Symfony to use it in your view like this :

{% form_theme form 'form/fields.html.twig' %}

Upvotes: 1

Rohit Patil
Rohit Patil

Reputation: 125

My View is looks like

enter image description here

My Updated Entity is

enter image description here

Upvotes: 0

Related Questions