场上有动态的表格活动

时间:2017-11-21 17:58:47

标签: forms symfony events

我遇到了FormEvents的问题。我有两个实体User和Personnel(fonction,statut),在UserType中有一个字段PersonnelType。 我希望根据statut的值动态填充字段功能。

为此,我使用FormEvent,就像文档说:

PersonnelType类:

<?php

namespace UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;

class PersonnelType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {   
        #$cod_cmp = $options['cod_cmp'];
        $builder->add('fonction', ChoiceType::class, array('attr' => array('class' => 'UserBundle:Fonction')))               
                ->add('statut',    ChoiceType::class, array(
                                'choices'   => array(
                                    'Administratif' => 'admi',
                                    'Professeur' => 'prof',                                   
                                ),
                                'preferred_choices' => array('prof'),

                                ));




            $formModifier = function (FormInterface $form, $statut) {

                $form->add('fonction', EntityType::class, array(
                            'class'        => 'UserBundle:Fonction',
                            'choice_label' => 'nom',
                            'query_builder' => function (EntityRepository $er) use($statut)  {
                                $qb = $er->createQueryBuilder('Fonction');
                                $qb->where('Fonction.statut = :statut')->setParameter('statut', $statut)
                                    ->orWhere('Fonction.statut = :no_statut')->setParameter('no_statut', '');

                                $qb->orderBy('Fonction.nom', 'ASC');
                                return $qb;
                            }

                    ));

                };

            $builder->get('statut')->addEventListener(
                FormEvents::POST_SUBMIT,
                function (FormEvent $event) use ($formModifier) {                

                    $formModifier($event->getForm()->getParent(), $event->getForm()->getData());
                }
            );
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'UserBundle\Entity\Personnel',

        ));

        $resolver->setRequired(['cod_cmp']);
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'userbundle_personnel';
    }
}

UserType类:

<?php

namespace UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use FOS\UserBundle\Util\LegacyFormHelper;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Doctrine\ORM\EntityRepository;
USE Symfony\Component\Form\FormError;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use UserBundle\Entity\User;

class UserType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {#die($options['cod_cmp']);
        $builder->add('personnel', PersonnelType::class, 
                        array('required' => false, 
                              'label' => 'Nouveau Enseignant/Administratif',
                              'cod_cmp' => $options['cod_cmp']                            
                              ))
                ->add('username',    TextType::class, array('label' => 'Login'))              
                ->add('email',    TextType::class, array('label' => 'Adresse e-mail'))
                ->add('Enregistrer',      SubmitType::class);         
    }


    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'UserBundle\Entity\User',
            'cod_cmp' => null,
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'userbundle_user';
    }
}

PersonnelController.php

public function addPersonnelAction(Request $ request){

        $userManager = $this->get('fos_user.user_manager');
        $user = $userManager->createUser();

        $form = $this->createForm(UserType::class, $user);




        if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {


            $em = $this->getDoctrine()->getManager('app');

            $personnel = $user->getPersonnel();

            $fonction = $personnel->getFonction()->getNom(); die($fonction);

            $statut = $personnel->getStatut();

            $em->persist($personnel);
            $em->flush();

            $encoder_service = $this->get('security.encoder_factory');
            $encoder = $encoder_service->getEncoder($user);
            $encoded_pass = $encoder->encodePassword($user->getPassword(), $user->getSalt());
            $user->setPassword($encoded_pass); 

            $userManager->updateUser($user);

            return $this->redirectToRoute('user_login_success');                       
        }

        return $this->render('UserBundle:Prof:addProf.html.twig', array(
                                'form' => $form->createView(),

    ));
}

当我更改statut的值时,不会填充字段fonction。不幸的是,经过几个小时的互联网搜索,我仍然陷入困境。

非常感谢你的帮助

1 个答案:

答案 0 :(得分:2)

form events documentation中所述,您无法在FormEvents::POST_SUBMIT事件中向表单添加或删除字段。 您应该考虑收听FormEvents::SUBMIT并相应地修改您的代码。