Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\EventSubscriber;
- use App\Entity\Person;
- use App\Form\BaseType\BaseInputType;
- use App\Form\FormType\FormUmcnType;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use Symfony\Component\Form\FormEvent;
- use Symfony\Component\Form\FormEvents;
- use Symfony\Component\Form\FormInterface;
- use Symfony\Component\Validator\Constraints\NotBlank;
- class NumberTypeByCitizenshipSubscriber implements EventSubscriberInterface
- {
- public static function getSubscribedEvents(): array
- {
- return [
- FormEvents::PRE_SET_DATA => 'onPreSetData',
- FormEvents::POST_SUBMIT=> 'onPostSubmit'
- ];
- }
- public function onPreSetData(FormEvent $event): void
- {
- /** @var Person|null $data */
- $data = $event->getData();
- if (!$data) {
- return;
- }
- $this->setupNumberField(
- $event->getForm(),
- $data->getCitizenship()
- );
- }
- public function onPostSubmit(FormEvent $event)
- {
- $form = $event->getForm();
- $this->setupNumberField(
- $form->getParent(),
- $form->getData()
- );
- }
- private function setupNumberField(FormInterface $form, ?string $citizenship)
- {
- if (null === $citizenship) {
- $form
- ->add('number', FormUmcnType::class)
- ;
- return;
- }
- if ('domestic' == $citizenship) {
- $form
- ->add('number', FormUmcnType::class)
- ;
- return;
- }
- $form
- ->add('number', BaseInputType::class, [
- 'label' => 'label.passportNumber',
- 'help' => 'help.enterFullPassportNumber',
- 'attr' => [
- 'class' => 'passportNumber',
- 'autocomplete' => 'off'
- ],
- 'constraints' => [
- new NotBlank([
- 'message' => 'validate.required'
- ])
- ]
- ])
- ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement