Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 集合字段类型中的Symfony集合字段类型:添加/删除元素_Php_Jquery_Forms_Symfony_Collections - Fatal编程技术网

Php 集合字段类型中的Symfony集合字段类型:添加/删除元素

Php 集合字段类型中的Symfony集合字段类型:添加/删除元素,php,jquery,forms,symfony,collections,Php,Jquery,Forms,Symfony,Collections,我有一个关于symfony集合字段类型的问题。 由于symfony的cookbook条目用于处理集合字段类型,因此使用一种集合类型(添加、删除项目)是明确的。对我来说,当我想向集合类型中的集合类型添加/删除项时,就会出现问题。例子: 我有一个地址集合,它被添加到我的联系人类型类中 public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('addre

我有一个关于symfony集合字段类型的问题。 由于symfony的cookbook条目用于处理集合字段类型,因此使用一种集合类型(添加、删除项目)是明确的。对我来说,当我想向集合类型中的集合类型添加/删除项时,就会出现问题。例子: 我有一个地址集合,它被添加到我的联系人类型类中

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
      ->add('addresses', 'collection', array(
          'type' => new AddressType(),
          'allow_add' => true,
          'allow_delete' => true,
          'prototype' => true,
          'by_reference' => false,
        ))
     ;
}
此地址集合的类型为“AddressType”,如下所示:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
    // name = country
      ->add('name', 'text')
      ->add('cities', 'collection', array(
          'type' => 'text',
          'allow_add' => true,
          'allow_delete' => true,
          'prototype' => true,
      ))
    ;
}
我现在的目标是能够在表单中添加/删除新的“地址”(由于cookbook条目而起作用),但是对于每个地址,我还希望添加/删除城市

问题: 在遵循symfony页面的示例时,我应该在jquery中添加:

/ addresses
$collectionHolder = $('#ecommerce_user_contact_addresses');
收藏持有者。对于地址,我知道在这里写什么,因为视图中小部件中生成的数据原型具有该id。 但是我应该在这里为内部收藏写些什么呢? 当我写作时:

// cities
$collectionHolderCities = $('#ecommerce_user_contact_addresses___name___cities');
,它也来自父div的数据原型,在本例中描述了“cities”元素结构,我现在不知道如何添加/删除该元素的新元素

打电话时:

var prototype = $collectionHolderCities.data('prototype');
在我的'addCityForm(..)方法中,控制台将变量'prototype'记录为未定义。当我将原型记录在另一个方法'addAddressForm'中时,它正确地向我显示了原型结构

我应该将什么定义为城市“内部收藏”的收藏持有者,以及我需要做哪些改变才能使其发挥作用

  • 地址(带国家名称)
    • 城市
    • 城市
    • 城市
  • 地址(带国家名称)
    • 城市
    • 城市
问候

编辑: 我已经实现了js功能,现在以这种方式添加一个城市,它正在为document.ready上显示的第一个地址工作。我不知道这是否是最好的处理方法,欢迎使用任何其他方法:)

我在文件中找到了收款人。准备好了:

 // cities
$collectionHolderCities = $('#ecommerce_user_contact_addresses_0_cities');
然后,在我的addCityForm函数中,我进行了一些替换,以获得正确的新表单:

function addCityForm($collectionHolderCities, $newLinkCity) {
  // Get the data-prototype explained earlier
  var prototype = $collectionHolderCities.data('prototype');
  // get the new index
  var index = $collectionHolderCities.data('index');

  // replace 'cities_0' default value in prototype with 'cities__counter'
  prototype = prototype.replace("cities_0", "cities__counter__");
  prototype = prototype.replace("cities_0", "cities__counter__");
  // replace '0label__' with '__counter__label__'
  prototype = prototype.replace("0label__", "__counter__label__");
  // replace [cities][0] with [cities][__counter__]
  prototype = prototype.replace("[cities][0]", "[cities][__counter__]");


  // Replace '__counter__' in the prototype's HTML to
  // instead be a number based on how many items we have
  var trans = $('#city-trans').val();
  var newForm = prototype.replace(/__counter__/g,index);

  // Replace cities0 (cities + index) with cities_0 (cities + "_" + index)
  newForm = newForm.replace('cities'+index, 'cities_' + index);
  newForm = newForm.replace('cities'+index, 'cities_' + index);

  // var repStr = '0label__';
  //newForm = newForm.replace(repStr,  trans + ':');

  // increase the index with one for the next item
  $collectionHolderCities.data('index', index + 1);

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

  // add a delete link to the new form
  addCityFormDeleteLink($newFormS);
} 
“addAddressForm()”函数类似于symfony cookbook条目中的函数。
现在不知道如何为添加的地址添加城市:///p>我现在有一个针对两个集合的解决方案,但它不是递归的。有人有针对多个集合字段类型的递归解决方案吗?您最终找到了解决方案吗?我想我正在努力解决相同的问题()。希望收到您的来信!
jQuery(document).ready(function() {

  // addresses
  $collectionHolder = $('#ecommerce_user_contact_addresses');

  // add the "add a tag" anchor and li to the tags ul
  $collectionHolder.append($newLink);

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

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

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

  // add blank collections
  addAddressForm($collectionHolder, $newLink);


  // add city
  // cities
  $collectionHolderCities = $('#ecommerce_user_contact_addresses_0_cities');

  // add the "add a tag" anchor and li to the tags ul
  $collectionHolderCities.append($newLinkCity);

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



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

      addCityForm($collectionHolderCities, $newLinkCity);
  });

  addCityForm($collectionHolderCities, $newLinkCity);
});