src/Form/VehicleDocsCollectionType.php line 16

  1. <?php
  2. namespace App\Form;
  3. use \App\Entity\VehicleDocs;
  4. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  5. use Symfony\Component\Form\FormEvent;
  6. use Symfony\Component\Form\FormEvents;
  7. use \App\Entity\VehicleDocTypes;
  8. use Symfony\Component\Form\AbstractType;
  9. use Symfony\Component\Form\FormBuilderInterface;
  10. use Symfony\Component\OptionsResolver\OptionsResolver;
  11. use Symfony\Component\Form\Extension\Core\Type\FileType;
  12. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  13. class VehicleDocsCollectionType extends AbstractType {
  14.     /**
  15.      * {@inheritdoc}
  16.      */
  17.     public function buildForm(FormBuilderInterface $builder, array $options
  18.     {
  19.         $locale $options['locale'];
  20.         $label  $options['parent_label'];
  21.         $formModifier = function ($formVehicleDocs $doc null) use ($locale) {
  22.             $label $doc instanceof \App\Entity\VehicleDocs $doc->getDocType()->{"getdocName".$locale}() : ''//dynamically get doc type name based on the current locale
  23.             $form->add('receivedDocType'ChoiceType::class, array(
  24.                 'label'             => $label,
  25.                 'choices'           => VehicleDocs::RECEIVED_DOC_TYPES,
  26.                 'expanded'          => true,
  27.                 'placeholder'       => false,
  28.                 'multiple'          => false,
  29.                 'required'          => false
  30.             ));
  31.         };
  32.         //to specially handling each Entity in the collection we need to call the event listener
  33.         $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use($locale$label$formModifier)
  34.         {
  35.             $form  $event->getForm();
  36.             $child $event->getData();
  37.             $hideBrowseButton null;
  38.             //1st. special handlation for already created entity object
  39.             if ($child instanceof VehicleDocs && $child->getDocType() instanceof VehicleDocTypes ) {
  40.                 //if an existing VehicleDocs, the label passed from parent FormType will be overridden
  41.                 $label = ($locale == 'en') ? $child->getDocType()->getDocNameEn() : $child->getDocType()->getDocNameAr();
  42.                 //show image without browse button for documents only belong to any of following docTypes
  43.                 if ($child->getDocType()->getDocTypeId() == VehicleDocTypes::DOC_TYPES['vehicle_registration']) {
  44.                     $hideBrowseButton true;
  45.                 }
  46.                 //show receivedDocType radio buttons for documents only belong to any of following docTypes
  47.                 if ( $child->getDocType()->getDocTypeId() == VehicleDocTypes::DOC_TYPES['vehicle_registration']
  48.                         || $child->getDocType()->getDocTypeId() == VehicleDocTypes::DOC_TYPES['letter_of_renunciation']
  49.                         || $child->getDocType()->getDocTypeId() == VehicleDocTypes::DOC_TYPES['compensation_letter']) {
  50.                    $formModifier($event->getForm(), $child);
  51.                 }
  52.                 if ($child->getDocType()->getDocTypeId() == VehicleDocTypes::DOC_TYPES['inspection']) {
  53.                    $form ->add('vehicle'VehicleInspectionExpType::class, array('label'=>false));
  54.                 }
  55.             }
  56.             //2nd. show the default field whether there is already an entity object or not
  57.             $form->add('imagePath'FileType::class, array(
  58.                 'label'              => $label,
  59.                 'required'           => false,
  60.                 'image_property'     => 'imageWebPath',
  61.                 'hide_browse_button' => $hideBrowseButton,
  62.                 'attr'               => ['class' => 'docImg']
  63.             ))
  64.             ->add('clearImage'HiddenType::class, array(
  65.                 'label'    => false,
  66.                 'required' => false,
  67.                 'mapped'   => false
  68.             ))
  69.             ;
  70.         });
  71.     }
  72.     /**
  73.      * {@inheritdoc}
  74.      */
  75.     public function configureOptions(OptionsResolver $resolver) {
  76.         $resolver->setDefaults(array(
  77.             'data_class'         => VehicleDocs::class,
  78.             'translation_domain' => 'vehicles',
  79.             'locale'             => 'en',
  80.             'parent_label'       => false
  81.         ));
  82.     }
  83.     /**
  84.      * {@inheritdoc}
  85.      *
  86.      * @return null|string
  87.      */
  88.     public function getBlockPrefix() {
  89.         return 'appbundle_vehicledocscollection';
  90.     }
  91. }