【问题标题】:I want to repeat the same form x times in a loop and display it with twig我想在一个循环中重复相同的表格 x 次并用树枝显示它
【发布时间】:2018-02-20 20:06:16
【问题描述】:

我想循环重复表单并用twig显示 在控制器中:

<?php

foreach ($folder as $file) {
  $form [] = $this->get('form.factory')->create(UploadFileType::class, $file);
}

return $this->render('Admin/view-folder.html.twig', array(
  'form' => $form->createView()
));

//the formtype

class UploadFileType extends AbstractType
{
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
    $builder
      ->add('file', FileType::class)
      ->add('save', SubmitType::class);
  }
}

有人有解决办法吗?谢谢。

【问题讨论】:

  • RTFM?: twig.symfony.com/doc/2.x/tags/for.html 可能您必须为所有实例调用一次表单生成器
  • 您确定要 5 个单独的表单,还是要一个能够一次上传 5 个文件的表单?
  • 不,我希望相同的表格重复多次
  • @zarathoustra 好吧。显然,当 $form 是一个数组时,$form->createView() 不会很好地工作。我会首先将 $form 重命名为 $forms,以帮助跟踪正在发生的事情。

标签: php symfony formbuilder


【解决方案1】:

您应该按照Symfony documentation. 中的说明使用“CollectionType”字段

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多