我如何使用 symfony 4 中的 collectionType 创建一个包含多个表单作为字段的表单?
How can i create one form who contains multiple forms as fields using collectionType in symfony 4?
我想使用 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()
]);
}
这是 symfony 3.4 的一个例子,它在同一个页面上显示了两个不同的表单,只有一个提交按钮。注意户主是Member实体,household是Household实体:
$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',
)
);
所以解决方案是让集合类型不在我的表单中,而是在我的控制器中,所以我删除了:
`->add ('tickets', CollectionType::class, [
'entry_type' => TicketsType::class,
'allow_add' => true
])
`
然后当我想制作我的表单的集合类型时,我在我的控制器中写了这个:
$form = $this->createForm(CollectionType::class, $tickets, ['entry_type' => TicketsType::class] );
我想使用 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()
]);
}
这是 symfony 3.4 的一个例子,它在同一个页面上显示了两个不同的表单,只有一个提交按钮。注意户主是Member实体,household是Household实体:
$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',
)
);
所以解决方案是让集合类型不在我的表单中,而是在我的控制器中,所以我删除了:
`->add ('tickets', CollectionType::class, [
'entry_type' => TicketsType::class,
'allow_add' => true
])
`
然后当我想制作我的表单的集合类型时,我在我的控制器中写了这个:
$form = $this->createForm(CollectionType::class, $tickets, ['entry_type' => TicketsType::class] );