Advertisement
SentinelBL

NumberSubscriber

Dec 18th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\EventSubscriber;
  4.  
  5. use App\Entity\Person;
  6. use App\Form\BaseType\BaseInputType;
  7. use App\Form\FormType\FormUmcnType;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Form\FormEvent;
  10. use Symfony\Component\Form\FormEvents;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\Validator\Constraints\NotBlank;
  13.  
  14. class NumberTypeByCitizenshipSubscriber implements EventSubscriberInterface
  15. {
  16.  
  17. public static function getSubscribedEvents(): array
  18. {
  19. return [
  20. FormEvents::PRE_SET_DATA => 'onPreSetData',
  21. FormEvents::POST_SUBMIT=> 'onPostSubmit'
  22. ];
  23. }
  24.  
  25. public function onPreSetData(FormEvent $event): void
  26. {
  27. /** @var Person|null $data */
  28. $data = $event->getData();
  29. if (!$data) {
  30. return;
  31. }
  32. $this->setupNumberField(
  33. $event->getForm(),
  34. $data->getCitizenship()
  35. );
  36. }
  37.  
  38. public function onPostSubmit(FormEvent $event)
  39. {
  40. $form = $event->getForm();
  41. $this->setupNumberField(
  42. $form->getParent(),
  43. $form->getData()
  44. );
  45. }
  46.  
  47. private function setupNumberField(FormInterface $form, ?string $citizenship)
  48. {
  49. if (null === $citizenship) {
  50. $form
  51. ->add('number', FormUmcnType::class)
  52. ;
  53. return;
  54. }
  55. if ('domestic' == $citizenship) {
  56. $form
  57. ->add('number', FormUmcnType::class)
  58. ;
  59. return;
  60. }
  61. $form
  62. ->add('number', BaseInputType::class, [
  63. 'label' => 'label.passportNumber',
  64. 'help' => 'help.enterFullPassportNumber',
  65. 'attr' => [
  66. 'class' => 'passportNumber',
  67. 'autocomplete' => 'off'
  68. ],
  69. 'constraints' => [
  70. new NotBlank([
  71. 'message' => 'validate.required'
  72. ])
  73. ]
  74. ])
  75. ;
  76. }
  77.  
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement