How to repeat form elements for rows from database

127 views Asked by At

I have a form which works as I expect it to (code below) and I can populate this with one row from a database via Doctrine.

However, I'm struggling to see how to repeat the elements of this form to edit all rows (e.g. 20 of them) in one form; with the hope of updating all fields of all rows in a single form on a single page.

For example:

name[0]
description[0]
url[0]
name[1]
description[1]
url[1]
name[2]
description[2]
url[2]

I've see the use of collection's suggested but they don't seem to suit repeating all of the form from what I can understand.

Form:

namespace Acme\Bundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'name',
                'text',
                array(
                    'label' => 'Company Name'
                )
            )
            ->add(
                'description',
                'text',
                array(
                    'label' => 'Company Description'
                )
            )
            ->add(
                'url',
                'text',
                array(
                    'label' => 'Company URL'
                )
            )
            ->add(
                'Update Companies',
                'submit'
            );
    }


    public function getName()
    {
        return 'ContactType';
    }
}

Entity:

<?php

namespace Acme\Bundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Contacts
 *
 * @ORM\Table(options={"engine":"MyISAM", "collate":"utf8_general_ci", "charset":"utf8"})
 * @ORM\Entity
 */
class Contacts
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="text", nullable=true)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text", nullable=true)
     */
    private $description;

    /**
     * @var string
     *
     * @ORM\Column(name="url", type="text", nullable=true)
     */
    private $url;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Contacts
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Contacts
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set url
     *
     * @param string $url
     * @return Contacts
     */
    public function setUrl($url)
    {
        $this->url = $url;

        return $this;
    }

    /**
     * Get url
     *
     * @return string 
     */
    public function getUrl()
    {
        return $this->url;
    }
}
1

There are 1 answers

1
S.Thiongane On

I think the collections will suit your need.

Create a form class which have one field, the collection of your ContactType :

class MyFormClassType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'myField',
                'collection',
                array(
                    'type' => new ContactType ()
                )
            );
    } 

    public function getName()
    {
        return 'my_class';
    }
}

With this you can add, in the same form, a collection of Contact !

Read More, from the doc