src/Form/OrderFormType.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Form;
  3. use App\Entity\Order;
  4. use App\Entity\Package;
  5. use App\Repository\PackageRepository;
  6. use App\Utils\CommonConstants;
  7. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  8. use Symfony\Component\Form\AbstractType;
  9. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Symfony\Component\OptionsResolver\OptionsResolver;
  12. use Symfony\Component\Security\Core\Security;
  13. /**
  14.  * Class OrderFormType
  15.  * @package App\Form
  16.  */
  17. class OrderFormType extends AbstractType
  18. {
  19.     /**
  20.      * @var Security
  21.      */
  22.     private $security;
  23.     public function __construct(Security $security)
  24.     {
  25.         $this->security $security;
  26.     }
  27.     /**
  28.      * {@inheritdoc}
  29.      */
  30.     public function buildForm(FormBuilderInterface $builder, array $options)
  31.     {
  32.         $builder
  33.             ->add('package'EntityType::class, [
  34.                 'class' => Package::class,
  35.                 'query_builder' => function (PackageRepository $er) {
  36.                     $isCoach null == $this->security->getUser() ? false in_array(CommonConstants::ROLE_ROLE_COACH$this->security->getUser()->getRoles());
  37.                     return $er->createQueryBuilder('p')
  38.                         ->where('p.coach = :coach')
  39.                         ->setParameter('coach'$isCoach);
  40.                 },
  41.                 'multiple' => false,
  42.                 'expanded' => true,
  43.             ]);
  44.         if ($options['submit']) {
  45.             $builder->add('submit'SubmitType::class, [
  46.                 'label' => 'Valider',
  47.                 'attr' => ['class' => 'btn btn-success']
  48.             ]);
  49.         }
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      */
  54.     public function configureOptions(OptionsResolver $resolver)
  55.     {
  56.         $resolver->setDefaults([
  57.             'data_class' => Order::class,
  58.             'attr' => ['id' => 'orderForm'],
  59.             'submit' => null,
  60.         ]);
  61.     }
  62. }