Symfony Form Builder: How to dynamically add Fields to a Poll

I created a Symfony Poll-Bundle which has the Entitys Campaign->Block->Line->Field->PollResult.
So i have a CollectionType CampaignType which consists of many blocks.
One block consist of many Lines.
One Line consist of many Fields.
(One Line is for example the line for fever and the fields are for intensity, and the period from and due the fever occured)
And every Field has one PollResult which holds the Answer of the user who filled out the campaign.

Now i want to make the user to be able to add new Fields to a line to enter a second fever period for example.
So i need a ‘Add a Line’-Button for every Line that duplicates this line.

I used this documentation to do that.

First i added 'allow_add' => true to my LineType FormBuilder:

public function buildForm(FormBuilderInterface $builder, array $options)

    $builder->add('fields', CollectionType::class, array(
        'entry_type' => FieldType::class,
        'entry_options' => array('label' => false),
        'allow_add' => true,

Then i added this to my view with the data-prototype:

<ul class="fields" data-prototype=" {{ form_widget(line.fields.vars.prototype)|e('html_attr') }} ">
        {% for field in line.fields %}
                {% for pollResult in field.pollResults %}
                    <div class="formLabelDiv">{{ field.vars.value.getTranslationName(app.request.getLocale()) }}</div>
                    <div class="formWidgetDiv">{{ form_widget(pollResult) }}</div>
                {% endfor %}
        {% endfor %}

And last i added this jQuery code:

    var $collectionHolder;

    //setup an "add a Line" Link
    var $addLineButton = $('<button type="button" class="add_line_link">Add a Line</button>');
    var $newLinkLi = $('<li></li>').append($addLineButton);

    jQuery(document).ready(function (){
        //Get the ul that holds the collection of fields
        $collectionHolder = $('ul.fields');

        //add the "add a line" anchor and li to the tags ul

        //count the current form inputs we have, use that as the new index when inserting a new item
        $'index', $collectionHolder.find(':input').length);

        $addLineButton.on('click', function (e)){
            addFieldsForm($collectionHolder, $newLinkLi);

    function addFieldsForm($collectionHolder, $newLinkLi){
        //get the data-prototyp
        var prototype = $'prototype');

        //get the new index
        var index = $'index');

        var newForm = prototype;

        $'index', index+1);

        // Display the form in the page in an li, before the "Add a tag" link li
        var $newFormLi = $('<li></li>').append(newForm);


So now the ‘Add a Line’-Button should appear on the page, but it doesn’t! what am i doing wrong?
When i’m done with that how am i able to duplicate the whole line and not only add one Field to the added line (I want to have the three fields intensity, from and due)

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *