2

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

2
  • Could you show your AcmeBundle:Form:fields_child.html.twig? :) Commented Aug 15, 2014 at 16:25
  • Nothing to see there, it's an empty template where the missing block should be put to render the players. I tried several block names but couldnt find anything that works. Commented Aug 17, 2014 at 12:27

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Are you suggesting to make a form PlayerType and using it in the following matter? $builder->add('players', new PlayerType()); Where playertype will have entity as a parent type? The template will still require rendering of the checkboxes and so i will have to override the complete widget and dropping the default "choice" rendering block. Correct?
I will try to implement this in a couple of days, short on time now. I have created custom form types before but tbh i didnt think this was to solution for my problem. We will see
Not a form type, but field type (technically it's the same but ..), you should use it like this: $builder->add('players', 'your_custom_type', array(...))
0

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 %}
    ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.