src/Form/RevueManagement/EventSubscriber/AddReceiptDateFieldSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Form\RevueManagement\EventSubscriber;
  3. use Doctrine\ORM\EntityRepository;
  4. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Form\Extension\Core\Type\DateType;
  7. use Symfony\Component\Form\FormEvent;
  8. use Symfony\Component\Form\FormEvents;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. class AddReceiptDateFieldSubscriber implements EventSubscriberInterface
  11. {
  12.     private $requestStack;
  13.     public function __construct(RequestStack $requestStack)
  14.     {
  15.         $this->requestStack $requestStack;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [FormEvents::PRE_SET_DATA => 'onReceiptDate'];
  20.     }
  21.     public function onReceiptDate(FormEvent $event): void
  22.     {
  23.         $form $event->getForm();
  24.         $request $this->requestStack->getCurrentRequest();
  25.         if ('fr' == $request->getLocale()) {
  26.             $form->add('receiptDate'DateType::class, [
  27.                     'required' => true,
  28.                     'html5' => false,
  29.                     'widget' => 'single_text',
  30.                     'format' => 'dd/MM/yyyy',
  31.                     'help' => 'dd/mm/yyyy',
  32.                     'attr' => ['class' => 'pop-date'],
  33.                     'label' => 'numero.receiptDate',
  34.                 ]);
  35.         } else {
  36.             $form->add('receiptDate'DateType::class, [
  37.                     'required' => true,
  38.                     'html5' => false,
  39.                     'widget' => 'single_text',
  40.                     'format' => 'yyyy-MM-dd',
  41.                     'help' => 'yyyy-mm-dd',
  42.                     'attr' => ['class' => 'pop-date'],
  43.                     'label' => 'numero.receiptDate',
  44.                 ]);
  45.         }
  46.     }
  47. }