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

时间:2015-03-01 12:22:35

标签: php jquery forms symfony collections

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

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,
        ))
     ;
}

此地址集合的类型为#34; 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,
      ))
    ;
}

我的目标是能够添加/删除新的"地址"到表格(由于菜谱条目正在运作),但对于每个地址,我也想添加/删除城市。

问题: 当遵循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(..)'方法,控制台记录变量'原型'如未定义。当我使用另一种方法' addAddressForm'记录原型时,它正确地向我展示了原型结构。

我应该定义什么作为"内部集合的收集持有者"对于城市,我需要改变什么才能使这个工作?

  • 地址(带国名)
  • 地址(带国名)
    • 市 ...

问候。

修改 我已经实现了js函数来以这种方式添加City,它正在为document.ready上显示的第一个地址工作。我不知道这是否是处理它的最佳方式,欢迎任何其他方法:)

我在document.ready中获得了集合Holder:

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

然后,在我的addCityForm函数中,我做了一些替换以使newForm正确:

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);
} 

现在的问题是: 当我添加新的地址时,我无法将城市添加到此新添加的地址中。没有链接或默认城市输入字段。我该怎么处理?

添加新城市仅适用于document.ready上显示的第一个地址:

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);
});

&#39; addAddressForm()&#39;功能就像在symfony cookbook中一样。 现在不知道如何为添加的地址添加城市..:/

0 个答案:

没有答案