【问题标题】:Form collection with last array element具有最后一个数组元素的表单集合
【发布时间】:2015-10-09 12:06:40
【问题描述】:

我有两个实体:IssueNotes,一个issue可以有多个notes。我是这样定义它们的:

class Issue {
  // ...

  /**
   * @ORM\OneToMany(targetEntity="Note", mappedBy="issue")
   */
  protected $notes;

  public function getNotes() {
    return $this->notes->toArray();
  }

  public function addNote($note) {
    if (!$this->notes->contains($note)) $this->notes->add($note);
  }

  public function removeNote($note) {
    if ($this->notes->contains($note)) $this->notes->removeElement($note);
  }
}

class Note {
  // ...

  /**
   * @ORM\Column(type="string", nullable=false)
   */
  protected $description;

  /**
   * @ORM\ManyToOne(targetEntity="Issue", inversedBy="notes")
   * @ORM\JoinColumn(name="issue", referencedColumnName="id", nullable=false)
   */
  protected $issue;

  public function getDescription() // ...

  public function setDescription() // ...

  public function getIssue() // ...

  public function setIssue() // ...
}

我定义了一个 IssueType 来创建一个嵌入 NoteType 表单的表单:

class IssueType extends AbstractType {
  public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
        // ...
        ->add('notes', 'collection', ['type' => new NoteType()]);
  }
}

class NoteType extends AbstractType {
  public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('description', 'textarea');
  }
}

当我想创建一个新问题时,这很有效,因为注释数组只包含一个由 IssueController 创建的(空白)注释,用户可以使用表单填写它。但是,一旦创建了问题,我就有了一个查看问题页面,其中显示了所有注释,并且在底部有一个添加新注释的表单。问题是集合表单为新的(空白)笔记以及所有以前的笔记创建了一个输入(见下图)。

有什么方法我只能使用 Symfony 包含新(空白)笔记表单的输入,还是我需要使用 JavaScript 删除以前的笔记?

编辑:

这是 IssueController 的代码:

public function newAction(Request $request) {
  $em = $this->getDoctrine()->getManager();
  $issue = new Issue();
  $note = new Note();
  $issue->addNote($note);
  $note->setIssue($issue);

  $form = $this->createForm('issue', $issue);
  $form->handleRequest($request);

  // ...

  if ($form->isValid()) {
    $em->persist($issue);
    $em->persist($note);
    $em->flush();
    return $this->redirectToRoute(...);
  }

  return $this->render('/issue/new.html.twig', [
    'issue' => $issue,
    'form' => $form->createView()
  ]);
}

【问题讨论】:

  • 我想答案在于您的控制器。您能否向我们展示您初始化传递给表单构建器的实体的代码? (你的$this->createForm 声明)
  • 我刚刚为 IssueController 添加了代码。谢谢。
  • 不,仍然缺少一些东西。我不是在关注你的 $form->createView(),而是在关注 $form = $this->createForm(...) - 适合代替椭圆的变量是我特别感兴趣的。
  • 控制器中的“// ...”是我们需要看到的。
  • 对不起,我在选择代码时忘记复制了...您现在应该可以看到它。谢谢!

标签: javascript php forms symfony formcollection


【解决方案1】:

出现注释编辑框是因为您的表单指示为一对多关系创建可编辑集合。将某些东西放入表单类型意味着它通常是可编辑的,或者至少以表单的形式呈现。

如果您希望表单只能添加新注释,则必须删除表单的该集合属性。

而不是

->add('notes', 'collection', ['type' => new NoteType()]);

->add('newNote', 'textarea', ['label' => 'Add note', 'mapped' => false];

您的控制器也需要修改。

public function newAction(Request $request) {
    $em = $this->getDoctrine()->getManager();

    $issue = new Issue();
    $form = $this->createForm('issue', $issue);
    $form->handleRequest($request);

    if ($form->isValid()) {
        if ($newNote = trim($form['newNote']->getData())) {
            $note = new Note();

            $issue->addNote($note);
            $note->setIssue($issue);
            $note->setDescription($newNote);
            $em->persist($note); // Probably not needed as you're cascading persist
        }

        $em->persist($issue);

        $em->flush();
        return $this->redirectToRoute(...);
    }

    return $this->render('/issue/new.html.twig', [
    'issue' => $issue,
    'form' => $form->createView()
    ]);
}

这只会显示新笔记的输入。要显示现有笔记,您需要在视图中执行此操作,例如:

<h2>Notes</h2>
{% for note in issue.notes if note.description %}
    <p>{{ loop.index }}: {{ note.description }}</p>
{% else %}
    <p>No notes have been added for this issue.</p>
{% endfor %}

【讨论】:

    猜你喜欢
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 2015-12-07
    • 1970-01-01
    • 2021-02-14
    • 2018-05-06
    • 1970-01-01
    相关资源
    最近更新 更多