我试图创建一个表单,该表单可以接受可变数量的整数类型的元素,这些元素将存储在类型数组的实体字段中。
我有这个用于动态添加和删除表单中的数字但是当我做多次添加和删除并更新数组中的一些先前持久的数字时,通过在我的编辑表单中进行这种操作并保存来覆盖。我想它与数组索引有关。例如,如果我有以前持久化形式的1,2,3,4,并且我尝试在我的编辑表单中编辑相同的数组,那么它的数字为1,3,5只有3和5得到持久。
我想知道是否可以这样做,将集合字段作为整数类型。它们应该是其他类型的对象吗?
非常感谢任何帮助。
<script type="text/javascript">
var $collectionHolder;
// setup an "add a tag" link
var $addTagLink = $('<a href="#" id="add-another-number" class="btn btn-info glyphicon glyphicon-plus" role="button">Add</a>');
var $newLinkLi = $('<li class="list-group-item"></li>').append($addTagLink);
jQuery(document).ready(function() {
// Get the ul that holds the collection of tags
$collectionHolder = $('#number-fields-list');
// add a delete link to all of the existing tag form li elements
$collectionHolder.find('li').each(function() {
addTagFormDeleteLink($(this));
});
// ... the rest of the block from above
// Get the ul that holds the collection of tags
// add the "add a tag" anchor and li to the tags ul
$collectionHolder.append($newLinkLi);
// 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);
$addTagLink.on('click', function(e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// add a new tag form (see next code block)
addTagForm($collectionHolder, $newLinkLi);
});
});
function addTagForm($collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
var prototype = $collectionHolder.data('prototype');
// get the new index
var index = $collectionHolder.data('index');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);
// increase the index with one for the next item
$collectionHolder.data('index', index + 1);
// Display the form in the page in an li, before the "Add a tag" link li
var $newFormLi = $('<li class="list-group-item"></li>').append(newForm);
$newLinkLi.before($newFormLi);
addTagFormDeleteLink($newFormLi);
}
function addTagFormDeleteLink($tagFormLi) {
var $removeFormA = $('<a href="#" role="button" class="glyphicon glyphicon-remove remove-box"></a>');
$tagFormLi.append($removeFormA);
$removeFormA.on('click', function(e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// remove the li for the tag form
$tagFormLi.remove();
});
}
NumberType.php :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('number', 'collection', array(
'type' => 'integer',
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => false,
)
)
->add('create', 'submit')
;
}
Numbers.php :
class Number
{
/**
* @var integer
*/
private $id;
/**
* @var array
*/
private $numbers = array();
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set numbers
*
* @param array $numbers
* @return Number
*/
public function setNumbers($numbers)
{
$this->numbers = $numbers;
return $this;
}
/**
* Get numbers
*
* @return array
*/
public function getNumbers()
{
return $this->numbers;
}
}