javascript - 包含最后一个数组元素的表单集合

标签 javascript php forms symfony formcollection

我有两个实体:问题注释,并且一个问题可以有多个注释。我这样定义它们:

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 创建的一个(空白)注释,用户可以使用表单填写该注释。但是,创建问题后,我会看到一个显示所有注释的查看问题页面,并且底部有一个用于添加新注释的表单。问题在于收集表单为新的(空白)笔记以及所有以前的笔记创建了一个输入(见下图)。

Unwanted description input boxes

有什么方法可以让我只使用 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()
  ]);
}

最佳答案

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

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

而不是

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

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

您的 Controller 也需要修改。

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()
    ]);
}

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

<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 %}

关于javascript - 包含最后一个数组元素的表单集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33037877/

相关文章:

没有与数据库紧密耦合并使用存储过程的 PHP 框架?

php - 图像上传与形式发送内容

python - 如何修复 : Cannot resolve keyword 'user' into field. 选择为 : description, end_time、id、start_time、title Django 错误

javascript - 获取 Map 对象的最后一个键

javascript - 如何在 Pinterest 上分享 data-uri 图像?

javascript - 如何在变量 Json href 属性中插入值?

javascript - 无法修复聊天框底部的滚动条(div)

php - 从标题字符串中提取值

php - 使用外部表单登录 Joomla 1.5(不在 joomla 文件夹中,但在同一台服务器上)

javascript - 如何在每次进入 View 时调用 onInit()?