Symfony2 array of forms?

后端 未结 4 932
孤城傲影
孤城傲影 2021-02-10 09:06

Is it possible to create and render and array of forms I know about collections but they don\'t really fit in my idea?

What I want is something like this

Contro

4条回答
  •  误落风尘
    2021-02-10 09:26

    The action :

    $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()
            // ...
        }
    }
    

    And a better way to render :

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

提交回复
热议问题