src/Form/ParentInscriActiviteType.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\FormBuilderInterface;
  5. use Symfony\Component\OptionsResolver\OptionsResolver;
  6. use App\Entity\Membre;
  7. use App\Entity\Activite;
  8. use Doctrine\ORM\EntityRepository;
  9. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  10. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. class ParentInscriActiviteType extends AbstractType
  16. {
  17.     private $requestStack;
  18.     private $doctrine;
  19.     private $security;
  20.     private $translator;
  21.     public function __construct(RequestStack $requestStack,ManagerRegistry $doctrine,Security $security,TranslatorInterface $translator)
  22.     {
  23.         $this->requestStack $requestStack;
  24.         $this->doctrine $doctrine;
  25.         $this->security $security;
  26.         $this->translator $translator;
  27.     }
  28.     public function buildForm(FormBuilderInterface $builder, array $options): void
  29.     {
  30.         $user $this->security->getUser();
  31.         if ($user) {
  32.          $userId $user->getUserIdentifier();
  33.          dump($userId);
  34.         
  35.             
  36.          $membre $this->doctrine->getRepository(Membre::class)->findOneBy(['courriel' => $userId]);
  37.  
  38.          }
  39.         $activite = new Activite();
  40.         $builder
  41.         ->add('activite'EntityType::class, [
  42.             'label' => $this->translator->trans("Nom de l'activité"),
  43.             'label_attr' => ['class' => 'form-label'],
  44.             'class' => Activite::class,
  45.             'expanded' => true,
  46.             'multiple' => true,
  47.             'choice_label' => function (Activite $activite) {
  48.                 return sprintf(' %s / %s / %s',
  49.                     $this->translator->trans($activite->getThemeactivite() ,[], 'messages'$this->translator->getLocale()), 
  50.                     $this->translator->trans($activite->getNomactivite(),[], 'messages'$this->translator->getLocale()),
  51.                     $this->translator->trans($activite->getTypeactivite() ,[], 'messages'$this->translator->getLocale()),
  52.             );},
  53.             
  54.             'query_builder' => function (EntityRepository $er)use ($membre) {
  55.                 if ($membre->getType() == 'parent') {
  56.                     $trancheage = ['Adulte''0-5 ans'];
  57.                     return $er->createQueryBuilder('a')
  58.                         ->where('a.inscription = :inscription')
  59.                         ->andWhere('a.trancheage IN (:trancheage)')
  60.                         ->setParameter('inscription''Ouverte')
  61.                         ->setParameter('trancheage'$trancheage);
  62.                 }
  63.                 else{
  64.                     $trancheage = ['0-5 ans','5-12 ans','12 ans et plus'];
  65.                     return $er->createQueryBuilder('a')
  66.                         ->where('a.inscription = :inscription')
  67.                         ->andWhere('a.trancheage IN (:trancheage)')
  68.                         ->setParameter('inscription''Ouverte')
  69.                         ->setParameter('trancheage'$trancheage);
  70.                 }
  71.             },
  72.                
  73.             'attr' =>['class' => 'mb-2 form-control form-control-solid ' ],
  74.             'choice_attr' => function($choice$key$value) {
  75.                 return [
  76.                     'class' => 'form-check-input'// Common class for all choices
  77.                     'style' => 'display: inline-block; margin-right: 10px;min-height: 0;'// Adjust the styling as needed
  78.                 ];
  79.             },
  80.             
  81.            
  82.         ])
  83.         ->add('Enregistrer'SubmitType::class,[
  84.             'attr'=> [
  85.                 'class'=> 'btn btn-primary',
  86.         ]
  87.             ])
  88.         ;
  89.     }
  90.     public function configureOptions(OptionsResolver $resolver): void
  91.     {
  92.         $resolver->setDefaults([
  93.             'data_class' => Membre::class,
  94.         ]);
  95.     }
  96. }