Symfony/Twig radio style formbuilder

516 views Asked by At

I need to style each of radio button using Symfony form builder.

This is my part of my createFormBuilder:

->add('categoryId', 'entity', array(
                'class' => 'MyBundle:Category',
                'property' => 'name',
                'required' => false,
                'expanded' => true ))

And in my twig template:

{% for child in form.categoryId %}
    <div class="radio i-checks col-md-3">
        <label>{{ form_widget(child, {'attr': { 'class': '', 'value': '' } }) }} </label>
    </div>
{% endfor %}

How can I display name of category (now I get empty values)?

When I'm using child.get('name'), I get this error

Method "get" for object "Symfony\Component\Form\FormView" does not exist in ...

2

There are 2 answers

0
Gemmi On BEST ANSWER

I solved this problem by editing,

contoller:

->add('categoryId', 'entity', array( 'class' => 'MyBundle:Category', 'property' => 'name', 'expanded' => true, 'multiple' => false, 'choices' => $this->getDoctrine() ->getRepository('MyBundle:Category') ->findAll(), 'required' => true, ))

And in my twig gallery:

<div class="row"> {% for child in form.categoryId %} <div class="radio i-checks col-md-3"> <label>{{ form_widget(child, {'attr': { 'class': 'required', 'value': child.vars.value } }) }} {{ child.vars.label }} </label> </div> {% endfor %} </div>

I hope it will help someone. greetings.

1
Frank B On

I guess

child.name

(And i could not post this answer without entering at least 30 characters)