Sam
Sam

Reputation: 2647

Render custom entity type field in form (Symfony)

I got stuck while trying to customize the rendering of a specific field in my form. It looks like this:

$builder->add('players', 'entity', array(
        'class' => 'Acme\Bundle\Entity\Player',
        'expanded' => true,
        'multiple' => true,
        'required' => false,
    ));

The form itself is beeing rendered with a simple:

{% block form_content %}
    {% form_theme form 'AcmeBundle:Form:fields_child.html.twig' %}
    {{ form_widget(form) }}
{% endblock %}

Now inside fields_child.html.twig i'm extending from another form template but there is nothing special there. My HTML looks like this:

Players: - [checkbox-input] 1

Where 1 equals the id of the only player in the database. However instead of rendering the ID im trying to render his picture and full name after the checkbox. I have tried many combinations of the form theming to override it but failed each time. Could someone post the twig block to render what i want here?

Thanks

Upvotes: 2

Views: 8039

Answers (2)

Dincho Todorov
Dincho Todorov

Reputation: 350

You have to create custom form field type together with custom widget template for it.

http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html

Upvotes: 2

xurshid29
xurshid29

Reputation: 4210

I recently ran into this problem (was a little bit different situation. I needed to show products as table with checkboxes..), Form's child data always returned null value that's why I ended up with this (dirty:)) solution:

Controller action:

...
$productRepository = $entityManager->getRepository('VendorMyBundle:Product');

$products = [];
$formChildren = $productListForm->createView()->children;
foreach ($formChildren['products'] as $formProduct) {
    $formProductId = $formProduct->vars['value'];
    $productEntity = $productRepository->find($formProductId);
    $products[$formProductId] = $productEntity;
}
...
return $this->render('TEMPLATE', [
    'productListForm' => $productListForm->createView(),
    'products' => $products,
]);

Template:

...
     {% for productForm in productListForm.products %}
        {% set id = productForm.vars.value %}
        <tr>
            <td class="check">
                {{ form_widget(productForm) }}
            </td>

            <td class="photo">
                {% if products[id].getImages().isEmpty() == false %}
                    {% set productImage = products[id].getImages().first() %}
                    <img src="{{ productImage.getWebPath() | imagine_filter('st_product_cabinet_thumbnail') }}" />
                {% else %}
                    <span class="no-image">No image</span>
                {% endif %}
            </td>

            <td class="title">
                <a href="{{ path('ROUT', {'productSlug':products[id].getSlug()}) }}" target="_blank">{{ products[id].getName() }}</a>
            </td>

            <td class="status">
                {{ products[id].getStatusName(products[id].getStatus()) }}
            </td>

            <td class="price">
                <ul>
                    {% for productPrice in products[id].getPrices() %}
                        <li>{{ productPrice.getValue() ~ ' ' ~ productPrice.getCurrencyCode() }}</li>
                    {% endfor %}
                </ul>
            </td>
        </tr>
    {% endfor %}
    ...

Upvotes: 0

Related Questions