Symfony 2 form theme related entities

43 Views Asked by At

I'm using Symfony 2.4.

I have 2 entities having OneToMany and ManyToOne relationship.

I'm rendering the entities in a twig.

class SomeController extends Controller
{

    /**
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function listAction()
    {
        $em = $this->getDoctrine()->getManager('some_manager');
        $entities = $em->getRepository('SomeBundle:SomeClass')->findAll();

        return $this->render('SomeBundle:list.html.twig', ['entities' => $entities]);
    }
    ...

Straight forward stuff. Works fine, no issue there. It's more for completion.


The forms are like so:

class FirstType extends AbstractType
{
    /**
     * Build the form elements
     *
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('url', 'hidden')
            ->add('secondEntity', 'collection', ['type' => new SecondType()])
        ;
    }
    ...

class SecondType extends AbstractType
{
    /**
     * Build the form elements
     *
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('value')
            ->add('type')
        ;
    }

Again, works fine.


The twig

{% block content %}

    <form method="post" class="form form-horizontal">
        {{ form_start(form) }}
        {{ form_row(form._token) }}

        {{ form_errors(form.url) }}
        {{ form_widget(form.url) }}

        {{ form_errors(form.secondEntityGetterName) }}
        {{ form_widget(form.secondEntityGetterName) }}

        <div class="form-actions">
            <button type="submit" class="btn btn-primary">Submit</button>
        </div>

        {{ form_end(form) }}
    </form>
{% endblock content %}

Everything works good here too. However since they're related Symfony insists on displaying how many entities are related to it.

Something like this enter image description here

Notice the integers at the left side of the screenshot. I'd like to remove those and leave only the right side of the screenshot.

I have RTFM but it doesn't specify anything about related entities.

I've tried using various getters inside the twig in the hopes that I can render each element separately. So far it's more of a hassle rather than anything else.

I'm sure there must be a better way.

0

There are 0 best solutions below