Php Symfony单击“添加表单行”

Php Symfony单击“添加表单行”,php,forms,symfony,button,onclick,Php,Forms,Symfony,Button,Onclick,我正在尝试使用Symfony创建一个表单,以便能够通过单击按钮添加新行。我无法使用SubmitType类,因为在submit之后symfony无法添加新行。ButtonType类没有基本函数isClicked()。这是我的表格: <?php namespace App\Form; use App\Entity\Team; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\Extens

我正在尝试使用Symfony创建一个表单,以便能够通过单击按钮添加新行。我无法使用SubmitType类,因为在submit之后symfony无法添加新行。ButtonType类没有基本函数isClicked()。这是我的表格:

<?php

namespace App\Form;

use App\Entity\Team;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;

final class TeamFormType extends AbstractType
{

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('teamName', TextType::class, [
                'label' => false,
                'attr' => array(
                    'placeholder' => 'Wpisz Nazwe Drużyny'
                )
            ])
            ->add('teamCity', TextType::class, [
                'label' => false,
                'attr' => array(
                    'placeholder' => 'Wpisz Nazwe Miasta'
                )
            ])
            ->add('teamCoach', TextType::class, [
                'label' => false,
                'attr' => array(
                    'placeholder' => 'Wpisz Imie i Nazwisko Trenera'
                )
            ]);
        $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){
            $form = $event->getForm();

            for ($i = 1; $i <= 6; $i++) {
                $name = 'zawodnik' . $i .'';
                $form->add($name, TextType::class, [
                    'mapped' => false,
                    'label' => false,
                    'attr' => array(
                        'placeholder' => 'Imie i Nazwisko Zawodnika'
                    )
                ]);
            }
            $form->add('agreeTerms', CheckBoxType::class, [
                'mapped' => false,
                'required' => true,
                'label' => 'Zaakceptuj regulamin'
            ])
            ->add('Submit', SubmitType::class, [
                'label' => 'Wyślij Zgłoszenie'
            ])
            ->add('addRow', ButtonType::class, [
                'label' => 'Dodaj zawodnika'
            ]);
        });
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Team::class,
            'csrf_protection' => true,
            'csrf_field_name'=> '_token',
            'csrf_token_id' => 'team_item'
        ]);
    }
}

我建议您使用Javascript和css,创建行并将其隐藏,然后单击,让em显示,并使用修改css表的JS函数

如果您想动态地向表单添加行,您可能需要查看Symfony表单集合:
<?php


namespace App\Controller;


use App\Form\TeamFormType;
use Doctrine\DBAL\Types\TextType;
use Doctrine\ORM\EntityManagerInterface;
use http\Env\Request;
use http\Env\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class FormsController extends AbstractController
{
    public function createTeamForm(Request $request, EntityManagerInterface $entityManager) :Response
    {
        $teamEntity = new Team();
        $form = $this->createForm(TeamFormType::class);
        $form->handleRequest($request);
        if($form->isSubmitted() && $form->isValid()) {
            if($form->get('addRow')->isClicked()) {
                $form->add('something', TextType::class, [
                    'label' => false,
                    'attr' => array(
                        'placeholder' => 'Imie i Nazwisko zawodnika.'
                    )
                ]);
            }

            $entityManager->persist($teamEntity);
            $entityManager->flush();

            $this->addFlash('success', 'Pomyślnie Utworzono Drużyne');
            return $this->redirectToRoute('index');
        }
    }
}