4

I'm trying to create a form which will add a new text box every time the 'Add new box' link got clicked. I read through the following example. http://symfony.com/doc/current/reference/forms/types/collection.html

Basically I was following the example from the book. But when the page is rendered and I click on the link nothing happens. Any thoughts? Thanks.

This is my controller.

 public function createAction() {
    $formBuilder = $this->createFormBuilder();

    $formBuilder->add('emails', 'collection', array(
        // each item in the array will be an "email" field
        'type'   => 'email',
        'prototype' => true,
        'allow_add' => true,
        // these options are passed to each "email" type
        'options'  => array(
            'required'  => false,
            'attr'      => array('class' => 'email-box')
        ),
    ));
    $form = $formBuilder->getForm();

    return $this->render('AcmeRecordBundle:Form:create.html.twig', array(
        'form' => $form->createView(),
    ));
}

This is the view.

 <form action="..." method="POST" {{ form_enctype(form) }}>
{# store the prototype on the data-prototype attribute #}
<ul id="email-fields-list" data-prototype="{{ form_widget(form.emails.get('prototype')) | e }}">
{% for emailField in form.emails %}
    <li>
        {{ form_errors(emailField) }}
        {{ form_widget(emailField) }}
    </li>
{% endfor %}
</ul>

<a href="#" id="add-another-email">Add another email</a>
</form>

<script type="text/javascript">
// keep track of how many email fields have been rendered
var emailCount = '{{ form.emails | length }}';

jQuery(document).ready(function() {
    jQuery('#add-another-email').click(function() {
        var emailList = jQuery('#email-fields-list');

        // grab the prototype template
        var newWidget = emailList.attr('data-prototype');
        // replace the "$$name$$" used in the id and name of the prototype
        // with a number that's unique to our emails
        // end name attribute looks like name="contact[emails][2]"
        newWidget = newWidget.replace(/\$\$name\$\$/g, emailCount);
        emailCount++;

        // create a new list element and add it to our list
        var newLi = jQuery('<li></li>').html(newWidget);
        newLi.appendTo(jQuery('#email-fields-list'));

        return false;
    });
})
</script>
Dhanushka
  • 82
  • 1
  • 8
  • Hard to answer out of seeing the Console output from Chrome/Firebug. To my experience while doing mine, I assume the `data-prototype` did not get filled or something similar. Part of my point of view is to do the most generic possible and never use specific javascript (such as your example `#add-another-email`... Tell me if my answer helped you. – renoirb Jun 20 '12 at 16:01

2 Answers2

2

This problem can be solved by referring to the following link.

https://github.com/beberlei/AcmePizzaBundle

Here you will find the same functionality being implemented.

Ashwin Preetham Lobo
  • 1,856
  • 1
  • 14
  • 19
1

I've been through this too.

Answer and examples given to this question and the other question I found did not answer my problem either.

Here is how I did it, in some generic manner.

In generic, I mean, Any collection that I add to the form just need to follow the Form template loop (in a macro, for example) and that's all!

Using which convention

Form Type class

class OrderForm extends AbstractType
{
// ...

public function buildForm(FormBuilder $builder, array $options)
{
// ...
  $builder
  ->add('sharingusers', 'collection', array(
          'type' => new UserForm(),
          'allow_add' => true,
          'allow_delete' => true,
          'by_reference' => false,
          'required'=> false
  ));
// ...
}
}

JavaScript

/* In the functions section out of document ready */

/**
 * Add a new row in a form Collection
 *
 * Difference from source is that I use Bootstrap convention
 * to get the part we are interrested in, the input tag itself and not
 * create a new .collection-field block inside the original.
 * 
 * Source: http://symfony.com/doc/current/cookbook/form/form_collections.html
 */
function addTagForm(collectionHolder, newBtn) {
    var prototype = collectionHolder.attr('data-prototype');
    var p = prototype.replace(/\$\$name\$\$/g, collectionHolder.children().length);
    var newFormFromPrototype = $(p);
    var buildup = newFormFromPrototype.find(".controls input");
    var collectionField = $('<div class="collection-field"></div>').append(buildup);
    newBtn.before(collectionField);
}


/* ********** */
$(document).ready(function(){


   /* other initializations */


    /**
     * Form collection behavior
     *
     * Inspired, but refactored to be re-usable from Source defined below
     *
     * Source: http://symfony.com/doc/current/cookbook/form/form_collections.html
     */
    var formCollectionObj = $('form .behavior-collection');
    if(formCollectionObj.length >= 1){
        console.log('run.js: document ready "form .behavior-collection" applied on '+formCollectionObj.length+' elements');
        var addTagLink = $('<a href="#" class="btn"><i class="icon-plus-sign"></i> Add</a>');
        var newBtn = $('<div class="collection-add"></div>').append(addTagLink);
        formCollectionObj.append(newBtn);
        addTagLink.on('click', function(e) {
            e.preventDefault();
            addTagForm(formCollectionObj, newBtn);
        });
    }


   /* other initializations */

});

The form template

Trick here is that I would have had used the original {{ form_widget(form }} but I needed to add some specific to the view form and I could not make it shorter.

And I tried to edit only the targeted field and found out it was a bit complex

Here is how I did it:

            {# All form elements prior to the targeted field #}
            <div class="control-collection control-group">
                <label class="control-label">{{ form_label(form.sharingusers) }}</label>
                <div class="controls behavior-collection" data-prototype="{{ form_widget(form.sharingusers.get('prototype'))|escape }}">
                {% for user in form.sharingusers %}
                    {{ form_row(user) }}
                {% endfor %}
                </div>
            </div>
            {{ form_rest(form) }}
Community
  • 1
  • 1
renoirb
  • 569
  • 5
  • 15