src/Form/VehicleRegistrationHasDoc.php line 13

  1. <?php
  2. namespace App\Form;
  3. use App\Entity\VehicleDocs;
  4. use App\Entity\VehicleRegistrations;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\Form\FormEvent;
  8. use Symfony\Component\Form\FormEvents;
  9. use Symfony\Component\OptionsResolver\OptionsResolver;
  10. class VehicleRegistrationHasDoc extends AbstractType
  11. {
  12.     /**
  13.      * {@inheritdoc}
  14.      */
  15.     public function buildForm(FormBuilderInterface $builder, array $options)
  16.     {
  17.         $locale $options['locale'];
  18.         $formModifier = function ($form) use($locale) {
  19.             $form->add('registrationDoc'VehicleDocsCollectionType::class, array(
  20.                 'label'        => false,
  21.                 'data_class'   => VehicleDocs::class,
  22.                 'by_reference' => false,
  23.                 'locale'       => $locale
  24.             ));
  25.         };
  26.         //this will add registrationDoc field to the form before fetching form fields data from DB
  27.         $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($formModifier) {
  28.             // this is the entity, i.e. registrationDoc
  29.             $form$event->getForm();
  30.             $formModifier($form);
  31.         });
  32.         //this will add registrationDoc field to the form after the from is posted and before mapping the posted data to the FormType class
  33.         $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($formModifier) {
  34.             // this is the posted data as an array
  35.             $child $event->getData();
  36.             $form  $event->getForm();
  37.             if (isset($child['registrationDoc'])) {
  38.                 $formModifier($form);
  39.             }
  40.         });
  41.     }
  42.     
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public function configureOptions(OptionsResolver $resolver)
  47.     {
  48.         $resolver->setDefaults(array(
  49.             'data_class'         => VehicleRegistrations::class,
  50.             'translation_domain' => 'vehicles',
  51.             'locale'             => 'en'
  52.         ));
  53.     }
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     public function getBlockPrefix()
  58.     {
  59.         return 'appbundle_vehicleregistrationsdoc';
  60.     }
  61. }