我有根据Symfony docs创建的表单。如果我尽我所能,添加新模块不起作用 - 新项目将被忽略,并且不会在请求中发送。
class Web extends AbstractModifiableEntity
{
/**
*
* @ORM\OneToMany(targetEntity="SelectedModule", mappedBy="web", cascade={"persist"})
*/
private $modules;
public function setModules($modules)
{
$this->modules = $modules;
return $this;
}
public function getModules()
{
return $this->modules;
}
public function addModule(SelectedModule $module){
$module->setWeb($this);
$this->modules->add($module);
}
public function removeModule(SelectedModule $module){
$this->modules->removeElement($module);
}
}
class SelectedModule
{
/**
* @ORM\ManyToOne(targetEntity="Web", inversedBy="modules")
* @ORM\JoinColumn(name="web_id", referencedColumnName="id")
*/
private $web;
/**
* @ORM\ManyToOne(targetEntity="Module", inversedBy="moduleInstances")
* @ORM\JoinColumn(name="module_id", referencedColumnName="id")
*/
private $module;
}
class Module extends AbstractModifiableEntity
{
/**
* @ORM\OneToMany(targetEntity="SelectedModule", mappedBy="module")
*/
private $moduleInstances;
}
class EditWebType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('modules', 'collection', array(
'type' => new SelectedModuleType($this->modules),
'label' => 'administration.webs.form.modules',
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'prototype' => true
))
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Company\CoreBundle\Entity\Web',
));
}
}
class SelectedModuleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('module', 'entity', array(
'class' => 'ProfixwebCoreBundle:Module',
'choice_label' => 'name',
'query_builder' => $this->getModuleQueryBuilder(),
'label' => false
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Company\CoreBundle\Entity\SelectedModule',
));
}
}
在树枝上
<div>
<ul class="modules" data-prototype="{{form_widget(form.modules.vars.prototype)|e }}">
{% for child in form.modules.children %}
<li class="modules_inner">{{ form_widget(child) }}</li>
{% endfor %}
</ul>
</div>
<script type="text/javascript">
var $collectionHolder;
var $addModuleLink = $('<a href="#" class="add_module_link button">{% trans %}administration.modules.edit.addModule{% endtrans %}</a>');
var $newLinkLi = $('<li></li>').append($addModuleLink);
$('document').ready(function(){
$('.default-hide').hide();
$collectionHolder = $('ul.modules');
$collectionHolder.find('li').each(function() {
addTagFormDeleteLink($(this));
});
$collectionHolder.append($newLinkLi);
$collectionHolder.data('index', $collectionHolder.find(':input').length);
$addModuleLink.on('click', function(e) {
e.preventDefault();
addModuleForm($collectionHolder, $newLinkLi);
if($('#visiblePerex').hasClass('button_inactive')){
changeTransVisibility('perex', 'hide');
}
if($('#visibleBody').hasClass('button_inactive')){
changeTransVisibility('body', 'hide');
}
});
});
function addModuleForm($collectionHolder, $newLinkLi) {
var prototype = $collectionHolder.data('prototype');
var index = $collectionHolder.data('index');
var newForm = prototype.replace(/__name__/g, index);
$collectionHolder.data('index', index + 1);
var $newFormLi = $('<li class="translations_inner"></li>').append(newForm);
$newLinkLi.before($newFormLi);
addTagFormDeleteLink($newFormLi);
}
function addTagFormDeleteLink($tagFormLi) {
var $removeFormA = $('<a href="#" class="button">{% trans %}administration.modules.edit.remModule{% endtrans %}</a>');
$tagFormLi.append($removeFormA);
$removeFormA.on('click', function(e) {
e.preventDefault();
$tagFormLi.remove();
});
}
</script>
我不知道出了什么问题,我该怎么做。
编辑:
我在文件的其他部分遗漏了</div>
。