我对symfony2中的表单集有一个奇怪的问题。
我制作了一个表单集,为一个人增加奖励。 如果我尝试将奖励添加到控制器中的某个人,则会将其添加到表单中而不会出现问题,如果我提交表单,则会将其保留到数据库中。
问题在于我通过学说在形式中添加奖励。 它在表单中显示没有问题,但从未通过submitController。如果我转储完整的POST数据集,那么“第一”奖项就在那里,但增加的奖项无处可寻。
表单字段名称似乎是正确的: 现有字段的designer_form [awards] [0] [year] designer_form [awards] [1] [年]为添加的字段
奖励表似乎
class DesignerAwardForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('year','textarea');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Common\UserBundle\Entity\DesignerAwards',
));
}
public function getName()
{
return 'designeraward';
}
}
和设计师形式:
namespace Common\UserBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Common\UserBundle\Form\DesignerAwardForm;
use Common\MediaBundle\Form\MediaForm;
class DesignerDataForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$toolbar = array(
'toolbar' => array(
array(
'name' => 'basicstyles',
'items' => array('Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat'),
)));
$builder
->add('description','ckeditor', array('config' => $toolbar, ))
->add('titles','ckeditor', array('config' => $toolbar, ))
->add('expertise','ckeditor', array('config' => $toolbar, ))
->add('education','ckeditor', array('config' => $toolbar, ))
->add('work_experience','ckeditor', array('config' => $toolbar, ))
->add('publications','ckeditor', array('config' => $toolbar, ))
//->add('address', new \Common\ContactDataBundle\Form\AddressForm())
->add('awards', 'collection', array(
'type' => new DesignerAwardForm(),
'allow_add' => true,
'prototype' => true,
'by_reference' => false,))
;
}
public function getName()
{
return 'designer_form';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Common\UserBundle\Entity\Designer',
));
}
}
模板中的调用:
<ul class="awards" data-prototype="{{ form_widget(edit_form.awards.vars.prototype)|e }}">
{% for award in edit_form.awards %}
<li>{{ form_row(award.year) }}</li>
{% endfor %}
</ul>
控制器:
$oDesignerForm = $this->em->getRepository('CommonUserBundle:Designer')->find($id);
$oForm = $this->createForm(new \Common\UserBundle\Form\DesignerDataForm(), $oDesignerForm);
提交部分:
$oDesigner = $this->em->getRepository('CommonUserBundle:User')->find($id);
$this->checkAccessRights($oDesigner, "EDIT");
$oForm = $this->createForm(new \Common\UserBundle\Form\DesignerDataForm(), $oDesigner);
$request = $this->getRequest();
$oForm->bind($request);
if ($oForm->isValid()) {
javascript原型:
var collectionHolder = $('ul.awards');
// setup an "add a tag" link
var $addTagLink = $('<a href="#" class="add_tag_link">Add an award</a>');
var $newLinkLi = $('<li></li>').append($addTagLink);
jQuery(document).ready(function() {
// 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></li>').append(newForm);
$newLinkLi.before($newFormLi);
}
如果有人有建议,那就太棒了