duande1986 2015-08-18 03:17
浏览 63
已采纳

如何在Symfony 2中使用集合字段类型

I'have been trying to use thr vollevtion field type in my Symfony 2 form, but it doesn't work. I already asked the same question sometime ago, but I didn't get the answer I wanted. I reed the documentation again (http://symfony.com/doc/current/reference/forms/types/collection.html), but I still got the same probleme.

I already explane everything inside my other post, so here it is:

How to integrate multiple entity in form in Symfony 2

Please, someone help me!

  • 写回答

1条回答 默认 最新

  • dqu94359 2015-08-18 07:22
    关注

    Hopefully this helps you out

    ParentType:

    ->add('funding', 'collection', array(
                    'type' => new FundingType(),
                    'allow_add' => true,
                    'by_reference' => false,
                    'allow_delete' => true))
    

    ChildType:

    public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('type', 'text', array(   
                    'label' => 'Funding'));
    
        }
    
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Bundle\Entity\Funding',
                ));
        }
    

    Controller:

    $funding = new Funding();
    $project->getFunding()->add($funding);
    

    Twig

     <div class="funding" data-prototype="{{ form_widget(form.funding.vars.prototype)|e}}">
           {% for funding in form.funding %}
                  {{ form_row(funding.type) }}
           {% endfor %}
     </div>
    
    <script>
    // Funding
    
    // setup an "add a tag" link
    var $addFundingLink = $('<a href="#" class="add_funding_link">Add Funding type</a>');
    var $newLinkLi1 = $('<p></p>').append($addFundingLink);
    
    jQuery(document).ready(function() {
        // Get the ul that holds the collection of tags
       var $collectionHolder1 = $('div.funding');
    
        // add the "add a tag" anchor and li to the tags ul
        $collectionHolder1.append($newLinkLi1);
    
        // count the current form inputs we have (e.g. 2), use that as the new
        // index when inserting a new item (e.g. 2)
        $collectionHolder1.data('index1', $collectionHolder1.find(':input').length);
    
        $addFundingLink.on('click', function(e) {
            // prevent the link from creating a "#" on the URL
            e.preventDefault();
    
            // add a new tag form (see code block below)
            addFundingForm($collectionHolder1, $newLinkLi1);
        });
    
    
    });
    
    function addFundingForm($collectionHolder1, $newLinkLi1) {
        // Get the data-prototype explained earlier
        var prototype = $collectionHolder1.data('prototype');
    
        // get the new index
        var index1 = $collectionHolder1.data('index1');
    
        // Replace '$$name$$' in the prototype's HTML to
        // instead be a number based on how many items we have
        var newForm1 = prototype.replace(/__name__/g, index1);
    
        // increase the index with one for the next item
        $collectionHolder1.data('index1', index1 + 1);
    
        // Display the form in the page in an li, before the "Add a tag" link li
        var $newFormLi1 = $('<p></p>').append(newForm1);
    
        // also add a remove button, just for this example
        $newFormLi1.append('<a href="#" class="remove-funding">Delete</a>');
    
        $newLinkLi1.before($newFormLi1);
    
        // handle the removal, just for this example
        $('.remove-funding').click(function(e) {
            e.preventDefault();
    
            $(this).parent().remove();
    
            return false;
        });
    }
    
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败