基于文档:http://symfony.com/doc/2.8/form/dynamic_form_modification.html#form-events-submitted-data
我准备了动态生成的表格。一切正常,但只有当我使用表单来添加新数据(/new)时,当我使用相同的表单来编辑现有数据时 - 不工作
- “约会”的简单表格。它应该像这样工作:用户选择客户端,然后第二个“选择”正在填充正确的数据 - 取决于第一个选择的每个客户端。这工作正常,但只有当我尝试添加新约会时。当我尝试编辑号时。
类 AppointmentType 扩展 AbstractType
{
公共函数 buildForm(FormBuilderInterface $builder, array $options)
{
$建造者
->添加('名称')
->add('client', EntityType::class, array(
'class' => 'SystemAdminBundle:Client',
'占位符' => '',
));
$formModifier = function(\Symfony\Component\Form\FormInterface $form, Client $client)
{
$疾病 = 数组();
if($client !== null) {
$diseases = $client->getDiseases();
}
$form->add('disease', EntityType::class, array(
'class' => 'SystemAdminBundle:Disease',
'占位符' => '',
'选择' => $疾病,
));
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
函数 (FormEvent $event) 使用 ($formModifier) {
$data = $event->getData();
$formModifier($event->getForm(), $data->getClient());
}
);
$builder->get('client')->addEventListener(
FormEvents::POST_SUBMIT,
函数 (FormEvent $event) 使用 ($formModifier) {
$client = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $client);
}
);
}
公共函数configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(数组(
'data_class' => 'System\AdminBundle\Entity\Appointment'
));
}
}
- 约会控制器 - 这是添加新约会和编辑的功能。对于“新”我的代码有效,对于“编辑”没有。
公共函数 newAction(请求 $request)
{
$约会=新约会();
$form = $this->createForm(AppointmentType::class, $appointment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $request->request->get('约会');
if(array_key_exists('name', $data)) {
$em = $this->getDoctrine()->getManager();
$em->坚持($约会);
$em->flush();
return $this->redirectToRoute('appointment_show', array('id' => $appointment->getId()));
}
}
return $this->render('appointment/new.html.twig', array(
'约会' => $约会,
'form' => $form->createView(),
));
}
公共功能editAction(请求$request,约会$appointment)
{
$deleteForm = $this->createDeleteForm($appointment);
$约会=新约会();
$editForm = $this->createForm('System\AdminBundle\Form\AppointmentType', $appointment);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$data = $request->request->get('约会');
if(array_key_exists('name', $data)) {
$em = $this->getDoctrine()->getManager();
$em->坚持($约会);
$em->flush();
return $this->redirectToRoute('appointment_show', array('id' => $appointment->getId()));
}
return $this->redirectToRoute('appointment_edit', array('id' => $appointment->getId()));
}
return $this->render('appointment/edit.html.twig', array(
'约会' => $约会,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
- 查看“新”约会
{% 块内容 %}
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
window.onload = 函数(){
var $sport = $('#appointment_client');
$sport.change(函数() {
var $form = $(this).closest('form');
变量数据 = {};
数据[$sport.attr('name')] = $sport.val();
数据['约会[_token]'] = $('#appointment__token').val();
$.ajax({
网址:$form.attr('action'),
类型:$form.attr('method'),
数据:数据,
成功:函数(html){
$('#appointment_disease').replaceWith(
$(html).find('#appointment_disease')
);
}
});
});
};
{% 端块 %}
- 查看“编辑”约会 - 几乎与“新”约会相同
{% 块内容 %}
{{ form_start(edit_form) }}
{{ form_widget(edit_form) }}
{{ form_end(edit_form) }}
window.onload = 函数(){
var $sport = $('#appointment_client');
$sport.change(函数() {
var $form = $(this).closest('form');
变量数据 = {};
数据[$sport.attr('name')] = $sport.val();
数据['约会[_token]'] = $('#appointment__token').val();
$.ajax({
网址:$form.attr('action'),
类型:$form.attr('method'),
数据:数据,
成功:函数(html){
$('#appointment_disease').replaceWith(
$(html).find('#appointment_disease')
);
}
});
});
};
{% 端块 %}