简体   繁体   中英

Collection as Symfony Form

I have a collection of entities called FooCollection. This collection is build from a API response. I don't use doctrine. The entities are instance of class Foo and each hold a class named Bar. I generate in the template a formular list like:

<form>
    <ul>
    {% for foo in fooCollection %}
        <li><input type="checkbox" name="foo[{{foo.id}}]" value="1"> <label>{{foo.bar.title}}</label></li>
    {% endfor %}
    </ul>
</form>

Is it possible to use Symfony2 Forms to create this form list and how can I do that?

Here are the example entities and collections:

<?php

class Foo
{
    private $bar;
    private $id;

    public function getId()
    {
        return $this->id;
    }

    public function setBar(Bar $bar)
    {
        $this->bar = $bar;
    }

    public function getBar()
    {
        return $this->bar;
    }
}

class Bar
{
    private $title;

    public function getTitle()
    {
        return $this->title;
    }
}

class FooCollection extends \Doctrine\Common\Collections\ArrayCollection
{
}

You can use the "choice" field type for dropdowns.

Simply SELECT the instances of Foo into an array, and pass this into the choices option of the ChoiceType to populate the dropdown.

See: http://symfony.com/doc/current/reference/forms/types/choice.html#choices

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM