Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8832959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:39:43+00:00 2026-06-14T08:39:43+00:00

I have problem with rendering forms in Twig. I’m trying to embed a collection

  • 0

I have problem with rendering forms in Twig. I’m trying to embed a collection of forms. When I render the collection, it is not shown, while it is shown the name of form. The aim was to make the forms with an add button to add at runtime the form for each element of the collection. I get a look to the Symfony Docs and I think I followed it step by step.

This is my controller:

function new_resultAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();

    $test = $em->getRepository('LadelaOdeskTesterBundle:Test')->find($id);
    $categories =  
      $em->getRepository('LadelaOdeskTesterBundle:Category')->findByTest($test);
    if (!$test) {
        throw $this->createNotFoundException('Unable to find Test entity.');
    } 

    $resultCategory = new Category();


    $form = $this->createForm(new CategoryType() , $resultCategory);

    $request = $this->getRequest();

    if ('POST' === $request->getMethod()) {

        $form->bindRequest($request);

        if ($form->isValid()) {

            $em->persist($resultCategory);                
            $em->flush();

            $this->get('session')->setFlash('success', 'New result Categories were 
          saved!');
            return $this->redirect($this->generateUrl('questions', array(
                'id' => $resultCategory->getId(),
            )));
        }
    }                 
  return array(
        'test' => $test,
        'categories' =>$categories,

        'form' => $form->createView(), 


      );

}

My forms:

 class ResultCategoryType extends AbstractType
    {
  public function buildForm(FormBuilderInterface $builder, array $options)
   {
    $builder->add('value', 'integer');
    $builder->add('category', 'entity', array(
            'class'         => 'Ladela\OdeskTesterBundle\Entity\Category',
            'query_builder' => function ($repository) { return 
     $repository->createQueryBuilder('p')->orderBy('p.name', 'ASC'); },
             'property' => 'name' ,
               'expanded' => false ,
               'multiple' => false , ));


 }

  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
    $resolver->setDefaults(array(
        'data_class' => 'Ladela\OdeskTesterBundle\Entity\ResultCategory'
    ));
  }

  public function getName()
{
    return 'ResultCategory';
}
 }

 class CategoryType extends AbstractType
 {
  public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name')
        ->add('resultCategories','collection', array(
            'type' => new ResultCategoryType(),
            'allow_add' => true,
            'by_reference' => false,
            'prototype' => true,

        ))
    ;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Ladela\OdeskTesterBundle\Entity\Category'
    ));
}

public function getName()
{
    return 'category';
}

}

And my twig file:

<form method="post" action="{{ path('questions',{'id': test.id })  }}" {{   
  form_enctype(form) }} >    

  <ul class="tags" data-prototype="{{ 
  form_widget(form.resultCategories.vars.prototype)|e 
   }}">
    {# iterate over each existing tag and render its only field: name #}
    {% for ResultCategory in form.resultCategories %}
        <li>{{ form_row(ResultCategory.category) }}</li>
    {% endfor %}
<a href="#" class="add_tag_link">Add a tag</a>

</ul>        
{{ form_rest(form) }}{# form.items's prototype is rendered twice #}
{{ form_errors(form) }}
<input type="submit" value ="add" />
</form> 

 {% block javascripts %}


<script type="text/javascript">
// Get the div that holds the collection of tags
var collectionHolder = $('ul.tags');

 // setup an "add a tag" link
   var $addTagLink = $('<a href="#" class="add_tag_link">Add a tag</a>');
  var $newLinkLi = $('<li></li>').append($addTagLink);

jQuery(document).ready(function() {
// add the "add a tag" anchor and li to the tags ul
collectionHolder.append($newLinkLi);

$addTagLink.on('click', function(e) {
    // prevent the link from creating a "#" on the URL
    e.preventDefault();

    // add a new tag form (see next code block)
    addTagForm(collectionHolder, $newLinkLi);
   });
 });
  // Get the div that holds the collection of tags
 var collectionHolder = $('ul.tags');

  // setup an "add a tag" link
 var $addTagLink = $('<a href="#" class="add_tag_link">Add a tag</a>');
 var $newLinkLi = $('<li></li>').append($addTagLink);

  jQuery(document).ready(function() {
 // add the "add a tag" anchor and li to the tags ul
  collectionHolder.append($newLinkLi);

 $addTagLink.on('click', function(e) {
    // prevent the link from creating a "#" on the URL
    e.preventDefault();

    // add a new tag form (see next code block)
    addTagForm(collectionHolder, $newLinkLi);
   });
  });

  {% endblock %}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:39:44+00:00Added an answer on June 14, 2026 at 8:39 am

    The collection of forms is not rendered since you don’t initiate it! See the official docs. There are “dummy” tags object used to create the tag forms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem SIMILAR to preventing form data reposting, but not quite the same
i have a little problem with the rendering of my forms. I would like
I have a Django forms dilemma. When rendering a form in a template, I
I have a problem with rendering PHP templates instead Twig templates in Symfony2. The
I have a problem with rendering my html page by the same browsers in
Ok so I have a problem with rails and rendering partials. I have a
Ok, here's a problem I'm having. I have Lua bindings to a rendering engine
I have problem with show or hide form in Window Form Application. I start
I have problem while loading data into html select when users press or click
I have a problem rendering the login page. I have followed this turtorial: http://www.dixis.com/?p=352

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.