0

I have this form element I want to render without a label, but I can't find the way...

$builder
    ->add('gender', 'choice', array(
        'expanded'   => true,
        'choices'    => array(
            'Male' => 'm',
            'Female' => 'f',
            )
        ))
;

some help please, this isn't working:

{% block choice_widget %}
    {% spaceless %}
        {% for child in form %}
            <input type="radio" value="{{ child.get('value') }}">
        {% endfor %}
    {% endspaceless %}
{% endblock choice_widget %}

I'm getting Array to string conversion

What I wanna do is an image based gender selector, just clicking an image to make the selection.

Xavi
  • 205
  • 6
  • 12
  • I'm thinking now that I could render just the label to load an image inside and to hide the input. But I have the same problem, how can I hide just the input and to show the label? – Xavi Nov 14 '12 at 07:31
  • How do you render your form view ? Can you paste the twig code ? – AdrienBrault Nov 14 '12 at 08:25

1 Answers1

5

When using the form component, do not ever render form fields yourself, always rely on the form_ helpers as described in the form documentation.

In your case, this should work:

{{ form_label(form.gender) }}
{{ form_errors(form.gender) }}

{% for choiceFormView in form.gender %}
    {{ form_widget(choiceFormView) }}
{% endfor %}
AdrienBrault
  • 7,747
  • 4
  • 31
  • 42
  • 1
    use `form_label` instead of `form_widget` in the for loop – AdrienBrault Nov 14 '12 at 11:29
  • I used this, but instead of seeing radio buttons, it shows the options as drop down box?? how can I use radio buttons? – myTD Jan 31 '13 at 05:23
  • @AdrienBrault I'm trying to customize the html for the label when it is a widget label as opposed to the row label. How can I differentiate between the two? I.e., how do I recognise the label is for a radio button and not for a general form row? – Christian Vermeulen May 01 '19 at 11:52