0

I'm trying to make a custom form in Symfony 2. I've got a Entity field type that I'm trying to render as expanded / multiple. The default rendering for each entity is something like the following pseudo code:

<input type="checkbox" value='$entity->id'><label>$entity->id</label>

This is pretty terrible. I'd like to get symfony to render each entity with something more detailed like:

<div>
    <input type="checkbox" value='$entity->id'><label><strong>$entity->name</strong>
    <div>$entity->detail</div></label>
</div>

The documentation doesn't mention how to access specific fields of an entity when rendering a form. Does anyone have an idea of how to tackle this?

Thanks!

2
  • Basically you are trying to customize form rendering, for that just follow documentation - symfony.com/doc/current/cookbook/form/form_customization.html Commented May 1, 2014 at 17:53
  • Basically yes, technically no. The documentation doesn't cover how to render an entity field separately from the entity widget. Plus the link you added, doesn't cover entity types at all. Commented May 1, 2014 at 22:52

3 Answers 3

1

You can access specific fields on an entity like this - {{ form_widget(form.your_choice_field.0) }} for the first item, {{ form_widget(form.your_choice_field.1) }} for the second and so on.

your_choice_field is a form field, which could be choice or entity.

{{ form_widget(form.your_choice_field.0) }} allows you to access individual items in choices array.

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

1 Comment

This would be useful, but I'm having trouble getting it working. Should it be form_widget instead of form.widget? Then, can you be more explicit about what "your_entity" is? Is that the entity name? The name of the entity field? Lets say I have a "Priority" entity with my entity field type called "priorities".
1

The default layout for all forms, if you use the full stack framework, is placed in

vendor\symfony\symfony\src\Symfony\Bridge\Twig\Resources\views\Form\form_div_layout.html.twig

you can see what happens there which is that entity is rendered as

{% block choice_widget_expanded %}
{% spaceless %}
    <div {{ block('widget_container_attributes') }}>
    {% for child in form %}
        {{ form_widget(child) }}
        {{ form_label(child) }}
    {% endfor %}
    </div>
{% endspaceless %}
{% endblock choice_widget_expanded %}

if you want to owerwrite rendering of a row of an entity field, as @Manocho mentioned, you can refer to http://symfony.com/doc/current/cookbook/form/form_customization.html and overwrite that block in your template file and then add

{% form_theme form _self %}

so twig will search for overwritten blocks in the same twig file it is rendered in

Comments

0

Have you tried adding property option when adding your entity field as described here? You can also add __toString method to your entity.

1 Comment

This neatly accomplishes half of what I need, and the rest a little more ugly. I'm hoping I can get what @dmnptr answered with for the other half. The two together make a fairly elegant solution.

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.