我有一个页面,其中包含隐藏模式的表单。 表单来自具有不同ID的同一实体。 显示模态作品。对于我要显示的每个模态,输入中的数据都可以。 问题是:提交表单时,所有表单(包括隐藏的表单)都将保留。
//EditServiceType.php
namespace Guillaume\PartnerManagerBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
class EditServiceType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', null, array(
'label_attr' => array('class' => 'col-form-label'),
'attr' => array('class' => 'form-control')
))
->add('type')
->add('version')
->add('connection_type', ChoiceType::class, array(
'choices' => array(
'Connected' => 2,
'Local' => 1,
)));
}/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Guillaume\PartnerManagerBundle\Entity\Service'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'edit_guillaume_partnermanagerbundle_service';
}
}
我的控制器的一部分
$formEdits = [];
$forms = [];
$serviceFormEdits = [];
foreach($services as $service) {
$serviceFormEdits[$service->getId()] = $em->getRepository('GuillaumePartnerManagerBundle:Service')->find($service);
$forms[$service->getId()] = $this->createForm('Guillaume\PartnerManagerBundle\Form\EditServiceType', $serviceFormEdits[$service->getId()]);
$formEdits[$service->getId()] = $forms[$service->getId()]->createView();
$forms[$service->getId()]->handleRequest($request);
if ($forms[$service->getId()]->isSubmitted() && $forms[$service->getId()]->isValid()) {
$em->persist($serviceFormEdits[$service->getId()]);
$em->flush();
$this->addFlash('notice', 'Service has been edited');
}
}
}
//(...)
return $this->render('GuillaumePartnerManagerBundle:customers:customer.html.twig', [
'formEdits' => $formEdits,
]);
我的树枝(模态)的一部分
{% for result in results %}
{% form_theme formEdits[result.service.id] 'bootstrap_4_horizontal_layout.html.twig' %}
<div class="modal fade" id="editEnvironment-{{ result.service.id }}" tabindex="-1" role="dialog"
aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title" id="exampleModalLongTitle">{{ 'Edit Environment'|trans }}</h2>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{ form_start(formEdits[result.service.id]) }}
<div class="form-group row">
{{ form_widget(formEdits[result.service.id]) }}
</div>
<div class="modal-footer row">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" value="cancel" class="btn btn-secondary" data-dismiss="modal">{{ 'Cancel'|trans }}</button>
<button type="submit" value="save" class="btn btn-primary">{{ 'Validate'|trans }}</button>
</div>
</div>
{{ form_end(formEdits[result.service.id]) }}
</div>
</div>
</div>
</div>
{% endfor %}
答案 0 :(得分:0)
最后我想通了。
在选项解析器中添加“ id”
$forms[$service->getId()] = $this->createForm('Guillaume\PartnerManagerBundle\Form\EditServiceType', $serviceFormEdits[$service->getId()], ['id' => $service->getId()]);
添加隐藏在formType中的输入
->add('id', HiddenType::class, array(
'data' => $options['id'],
'mapped' => false,
))
当提交有效时,我使用此ID
$postData = $request->request->get('edit_guillaume_partnermanagerbundle_service');
$postId = $postData['id'];
$forms[$postId]->handleRequest($request);
if ($forms[$postId]->isSubmitted() && $forms[$postId]->isValid()) {
$em->persist($serviceFormEdits[$postId]);
$em->flush($serviceFormEdits[$postId]);
//(...)