我有一个为每个“ joueur”生成表单的函数:
public function new(Request $oRequest): Response
{
$played = new Played();
$em = $this->getDoctrine()->getManager();
$lastTourn = $em->getRepository(Tournoi::class)->findOneBy([], ['id' => 'desc']);
$disputeds = $lastTourn->getDisputeds();
foreach($disputeds as $d){
$joueurs[] = $d->getJoueur();
}
foreach ($joueurs as $j)
{
$sUniqueFormName = 'Form' . $j->getId();
$oForm = $this->get('form.factory')->createNamed($sUniqueFormName, PlayedType::class, $played);
$aForms[$sUniqueFormName] = $oForm;
}
foreach ($aForms as $sFormName => $oForm)
{
$oForm->handleRequest($oRequest);
if ($oForm->isSubmitted() && $oForm->isValid())
{
//flush
}
$aFormViews[$sFormName] = $oForm->createView();
}
return $this->render('played/new.html.twig', [
'played' => $played,
'joueurs' => $joueurs,
'formulaires' => $aFormViews,
]);
然后,在一个Twig视图中呈现表格数组的管理:
<h1>Create new Played</h1>
{% for form in formulaires %}
{{ joueurs[loop.index0].nom }}
{{ form_start(form) }}
{{ form_row(form.max) }}
{{ form_row(form.score) }}
{{ form_row(form.points) }}
{% endfor %}
<input type="submit" formaction="{{ path('played_new') }}" value="Envoyer le formulaire">
问题是,如何在一个提交按钮中提交所有这些表格?
然后,我如何管理所有表单数据以保留实体?
您在这里有我的数据库关系:
表单类:
class PlayedType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
/*->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$form->add('joueur', null, array(
'data' => $event->getData() ?: options['joueur']
))*/
->add('max')
->add('score')
->add('points')
;
//});
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Played::class,
'joueur' => null
]);
}
}
class PlayedScoreType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
//->add('nom')
//->add('prenom')
// NOTE: Use form collection to allow multiple `played` forms per JoueurType
->add('played', CollectionType::class, [
'entry_type' => PlayedType::class,
'entry_options' => ['label' => false],
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Partie::class,
]);
}
}
控制器:
$mainForm = $this->createForm(PlayedScoreType::class, $lastPartie);
$mainForm->handleRequest($request);
return $this->render('played/new.html.twig', [
'formulaires' => $mainForm->createView(),
]);
答案 0 :(得分:0)
https://symfony.com/doc/current/form/embedded.html
嵌入表单就像定义两个或多个表单一样简单,并且在将表单字段添加到外部/主表单时,其一个或多个字段/元素是嵌套表单:
class PlayedType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('max')
->add('score')
->add('points');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Played::class
]);
}
}
class JoueurType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nom')
->add('prenom')
// NOTE: Use form collection to allow multiple `played` forms per JoueurType
->add('played', CollectionType::class, [
'entry_type' => PlayedType::class,
'entry_options' => ['label' => false],
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Joueur::class,
]);
}
}
class TournoiType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
//
// NOTE: Use embeded form to connect the player with a tournement 1:1 -- otherwise use another form collection
$builder
->add('date')
->add('disputed', JoueurType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Tournoi::class,
]);
}
}