Search code examples
javascriptjquerysymfonycookbook

Symfony - Cookbook "add new tag" in embed form isn't working


I've followed the Symfony cookbook part called "How to Embed a Collection of Forms" and added this javascript function to have a link that adds a new tag field. The form is right, displaying the Task description field and one Tag name field.

When I click on the link, nothing happens. The JS code is shown in the source code of my page. By the way, Netbeans tells me "expected semicolon ; after )" in my link Add a tag

Here is my Controller code :

public function addTaskAction()
    {
        $task = new Task();
        $task->addTag(new Tag());

        $form = $this->createForm(new TaskType(), $task);

        $request = $this->get('request');
        if ($request->getMethod() === 'POST') {
            $form->bind($request);
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $em->persist($task);
                $em->flush();

            return $this->redirect($this->generateUrl('mytask_task',
                array('id' => $task->getId())));
            }
        }    
        return $this->render('MyTaskBundle:Question:taskadd.html.twig',
            array(
                'form' => $form->createView()
            ));
    }

And my form.html.twig :

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">     
    var collectionHolder = $('ul.tags');
    var $addTagLink = $('<a href="#" class="add_tag_link">Add a tag</a>');
    var $newLinkLi = $('<li></li>').append($addTagLink);

    jQuery(document).ready(function() {
        collectionHolder.append($newLinkLi);
        $addTagLink.on('click', function(e) {
            e.preventDefault();
            addTagForm(collectionHolder, $newLinkLi);
        });
        collectionHolder.find('li').each(function() {
            addTagFormDeleteLink($(this));
        });
    });
    function addTagForm(collectionHolder, $newLinkLi) {
        var prototype = collectionHolder.attr('data-prototype');
        var newForm = prototype.replace(/__name__/g,
 collectionHolder.children().length);
        var $newFormLi = $('<li></li>').append(newForm);
        $newLinkLi.before($newFormLi);

    }

    }       
</script>

    <form method="post" {{ form_enctype(form) }}>
    {{ form_row(form.description) }}

    <h3>Tags</h3>

    <ul class="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e }}">
    {% for tag in form.tags %}
        <li>{{ form_row(tag.name) }}</li>
    {% endfor %}
            <a href="#" class="add_tag_link" onclick="addTagForm()">Add a tag</a>
    </ul>

    {{ form_rest(form) }}
    <input type="submit" class="btn btn-primary" />
    </form>

Solution

  • I finally found an more simple JS code in this video : http://www.mistra.fr/tutoriel-symfony-2-les-formulaires-imbriques.html and it works fine !

       $(function(){
             var index = 0;
             var prototype = $('ul.answer').data('prototype');
             $('#addmore').on('click', function(){
                 var newForm = prototype.replace(/__name__/g, index++);
                 var newLi = $('<li></li>');
    
                 newLi.append(newForm);
                 $(this).before(newLi);
             });
         });