我想用一个带有symfony的提交按钮在一页中创建多个表单。我检查了文档,发现需要制作一个带有collectiontype字段的表单,但我不知道如何进行。我在两页中有两种形式,第一页用户告诉下一页将有多少种形式。我已经创建了我的第一个表单并且可以正常工作,我拥有第一个表单的数据,但是现在我可以根据提交的数据如何创建包含多个表单的第二个页面。 这是我的第一个表格:
class OrdersType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add ('tickets', CollectionType::class, [
'entry_type' => TicketsType::class,
'allow_add' => true
])
->add('numberOfTickets', ChoiceType::class, [
'attr' => [
'class' => 'form-control'
],
'choices' => [
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
]
])
->add('date', DateType::class, [
'attr' => [
'class' => 'form-control'
],
'widget' => 'single_text'
])
->add('type', ChoiceTYpe::class, [
'attr' => [
'class' => 'form-control'
],
'choices' => [
'Journée' => 1,
'Demi-journée(à partir de 14 heures)' => 0,
]
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Orders::class,
]);
}
}
我的第二个表单(我想在第二个页面中重复的表单):
class TicketsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('category', CheckboxType::class, [
'attr' => [
'class' => 'form-control'
]
])
->add('firstname', TextType::class, [
'attr' => [
'class' => 'form-control'
]
])
->add('lastname', TextType::class, [
'attr' => [
'class' => 'form-control'
]
])
->add('country', TextType::class, [
'attr' => [
'class' => 'form-control'
]
])
->add('dateOfBirth', DateType::class, [
'attr' => [
'class' => 'form-control'
],
'widget' => 'single_text'
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Tickets::class,
]);
}
}
和我的控制器:
/**
* @Route("/", name="home")
*/
public function home(Request $request, SessionInterface $session)
{
$Orders = new Orders();
$form = $this->createForm(OrdersType::class, $Orders);
$form->handleRequest($request);
if($form->isSubmitted()){
$formData = $form->getData();
$session->set('orders', $formData);
return $this->redirectToRoute('ticket');
}
$repo = $this->getDoctrine()->getRepository(Tickets::class);
$tickets = $repo->findAll();
return $this->render('louvre/home.html.twig', [
'form' => $form->createView()
]);
}
/**
* @Route("/ticket", name="ticket")
*/
public function ticket(Request $request, SessionInterface $session)
{
$data = $session->get('orders');
$Orders = new Orders()
for ($i=0; $i<$data->getNumberOfTickets() ;$i++){
echo 'test';
$tickets = new Tickets();;
$form = $this->createForm(OrdersType::class, $Orders);
$form->handleRequest($request);
};
$repo = $this->getDoctrine()->getRepository(Tickets::class);
$tickets = $repo->findAll();
return $this->render('louvre/ticket.html.twig', [
'form' => $form->createView()
]);
}
答案 0 :(得分:0)
这是来自symfony 3.4的示例,该示例使用一个提交按钮在同一页面上显示两种不同的形式。请注意,户主是成员实体,而家庭是家庭实体:
$form = $this->createForm(HouseholdType::class, $household);
$formHead = $this->createForm(MemberType::class, $head);
$form->handleRequest($request);
$formHead->handleRequest($request);
if ($form->isSubmitted() && $form->isValid() && $formHead->isSubmitted() && $formHead->isValid()) {
//relevant logic
}
return $this->render(
'Household/new.html.twig',
array(
'formType' => 'New Household',
'form' => $form->createView(),
'formHead' => $formHead->createView(),
'title' => 'New Household',
)
);
答案 1 :(得分:0)
所以解决方案是使collectiontype不是在我的表单中,而是在我的控制器中,所以我删除了:
`->add ('tickets', CollectionType::class, [
'entry_type' => TicketsType::class,
'allow_add' => true
])
`
然后,当我想创建表单的集合类型时,请在控制器中写上它:
$form = $this->createForm(CollectionType::class, $tickets, ['entry_type' => TicketsType::class] );