Symfony Forms - 添加输入数组

时间:2018-05-30 09:11:29

标签: forms symfony symfony4

所以我正在尝试为我的表单创建一个输入数组,但是在文档中没有找到任何资源(如果是这样的话我可能会错过一些链接)。

简而言之,我想渲染这样的东西:

<select name="my-select" id="some-select">
    <option value="a">A</option>
    <option value="b">B</option>
    <option value="c">C</option>
</select>

<button type="button" class="btn btn-primary" id="add-new-option">
    Add Another Option
</button>

所以想法是用户可以选择选项A,点击按钮将选项C和A添加到实体。

选项结果存储在如下表格中:

entity_id  option_value
1          A
1          C
2          B
2          A

我想这样做:

foreach ($arrayInput as $key => $value)
{
    $option = new Option();
    $option->setEntityId($entity->getId());
    $option->setOptionValue($value);
}

在控制器中。

但我有一种可怕的感觉,已经有办法做到这一点,我一直无法找到它。否则我必须在我的树枝模板中使用HTML,并且我试图将其减少..

上述方法是实现我想要的最好方法吗?

我当前的实体:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\PageRepository")
 */
class Page
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=125)
     */
    private $title;

    /**
     * @ORM\Column(type="text")
     */
    private $content;

    /**
     * @ORM\Column(type="boolean")
     */
    private $approved;

    /**
     * @ORM\Column(type="datetime")
     */
    private $created_ts;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $last_edit_ts;

    /**
     * @ORM\Column(type="integer")
     */
    private $author_id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Tag")
     */
    private $tag;

    public function getTag(): ?Tag
    {
        return $this->tag;
    }

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

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

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getContent(): ?string
    {
        return $this->content;
    }

    public function setContent(string $content): self
    {
        $this->content = $content;

        return $this;
    }

    public function getApproved(): ?bool
    {
        return $this->approved;
    }

    public function setApproved(bool $approved): self
    {
        $this->approved = $approved;

        return $this;
    }

    public function getCreatedTs(): ?\DateTimeInterface
    {
        return $this->created_ts;
    }

    public function setCreatedTs(\DateTimeInterface $created_ts): self
    {
        $this->created_ts = $created_ts;

        return $this;
    }

    public function getLastEditTs(): ?\DateTimeInterface
    {
        return $this->last_edit_ts;
    }

    public function setLastEditTs(?\DateTimeInterface $last_edit_ts): self
    {
        $this->last_edit_ts = $last_edit_ts;

        return $this;
    }

    public function getAuthorId(): ?int
    {
        return $this->author_id;
    }

    public function setAuthorId(int $author_id): self
    {
        $this->author_id = $author_id;

        return $this;
    }
}

由于

1 个答案:

答案 0 :(得分:0)

您可以使用ChoiceType(对于字符串)或EntityType(对于实体)'multiple' => true