Symfony2数组的形式



是否有可能创建和渲染和数组的形式,我知道关于集合,但他们不适合我的想法?

我想要的是这样的

<标题>控制器h1> h1> div class="one_answers">

在数组中创建表单:

$data = $em->findAll();
for ($i = 0; $i < $n; $i++) {
    $forms[] = $this->container
        ->get('form.factory')
        ->createNamedBuilder('form_'.$i, new SomeType, $data)
        ->getForm()
        ->createView();
}
return $this->render(someView, array("forms" => $forms));

更新

正如edlouth所提到的,您可以创建单独命名的每个表单。

在数组中创建表单,但给每个表单一个唯一的名称。我把它改成formbuilder,这可能不适合你,但希望类似的东西会起作用。我也不确定放入new SomeType而不是'form',参见http://api.symfony.com/2.4/Symfony/Component/Form/FormFactory.html#method_createNamedBuilder。

$data = $em->findAll();
for ($i = 0; $i < $n; $i++) {
    $forms[] = $this->container
        ->get('form.factory')
        ->createNamedBuilder('form_'.$i, new SomeType, $data)
        ->getForm()
        ->createView();
}
return $this->render(someView, array("forms" => $forms));

Symfony3:

$datas = $em->findAll();
foreach ($datas as $key=>$data)
{
   $form_name = "form_".$key;
   $form = $this->get('form.factory')->createNamed( 
      $form_name, 
      SomeType::class, 
      $data
   );
   $views[] = $form->createView();
}
return $this->render(someView, ["forms" => $views]);

动作:

$forms = [];
foreach ($articles as $article) {
    $forms[$article->getId()] = $this->get('form.factory')->createNamed(
        'article_'.$article->getId(), // unique form name
        ArticleType::class,
        $article
    );
    $forms[$article->getId()]->handleRequest($request);
    if ($forms[$article->getId()]->isValid()) {
        // do what you want with $forms[$article->getId()]->getData()
        // ...
    }
}

和更好的渲染方式:

return $this->render('some_view.html.twig', [
    'forms' => array_map(function ($form) {
        return $form->createView();
    }, $forms),
]);

相关内容

  • 没有找到相关文章

最新更新