src/Form/VehicleDocsCollectionType.php line 16
<?php
namespace App\Form;
use \App\Entity\VehicleDocs;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use \App\Entity\VehicleDocTypes;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
class VehicleDocsCollectionType extends AbstractType {
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$locale = $options['locale'];
$label = $options['parent_label'];
$formModifier = function ($form, VehicleDocs $doc = null) use ($locale) {
$label = $doc instanceof \App\Entity\VehicleDocs ? $doc->getDocType()->{"getdocName".$locale}() : ''; //dynamically get doc type name based on the current locale
$form->add('receivedDocType', ChoiceType::class, array(
'label' => $label,
'choices' => VehicleDocs::RECEIVED_DOC_TYPES,
'expanded' => true,
'placeholder' => false,
'multiple' => false,
'required' => false
));
};
//to specially handling each Entity in the collection we need to call the event listener
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use($locale, $label, $formModifier)
{
$form = $event->getForm();
$child = $event->getData();
$hideBrowseButton = null;
//1st. special handlation for already created entity object
if ($child instanceof VehicleDocs && $child->getDocType() instanceof VehicleDocTypes ) {
//if an existing VehicleDocs, the label passed from parent FormType will be overridden
$label = ($locale == 'en') ? $child->getDocType()->getDocNameEn() : $child->getDocType()->getDocNameAr();
//show image without browse button for documents only belong to any of following docTypes
if ($child->getDocType()->getDocTypeId() == VehicleDocTypes::DOC_TYPES['vehicle_registration']) {
$hideBrowseButton = true;
}
//show receivedDocType radio buttons for documents only belong to any of following docTypes
if ( $child->getDocType()->getDocTypeId() == VehicleDocTypes::DOC_TYPES['vehicle_registration']
|| $child->getDocType()->getDocTypeId() == VehicleDocTypes::DOC_TYPES['letter_of_renunciation']
|| $child->getDocType()->getDocTypeId() == VehicleDocTypes::DOC_TYPES['compensation_letter']) {
$formModifier($event->getForm(), $child);
}
if ($child->getDocType()->getDocTypeId() == VehicleDocTypes::DOC_TYPES['inspection']) {
$form ->add('vehicle', VehicleInspectionExpType::class, array('label'=>false));
}
}
//2nd. show the default field whether there is already an entity object or not
$form->add('imagePath', FileType::class, array(
'label' => $label,
'required' => false,
'image_property' => 'imageWebPath',
'hide_browse_button' => $hideBrowseButton,
'attr' => ['class' => 'docImg']
))
->add('clearImage', HiddenType::class, array(
'label' => false,
'required' => false,
'mapped' => false
))
;
});
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => VehicleDocs::class,
'translation_domain' => 'vehicles',
'locale' => 'en',
'parent_label' => false
));
}
/**
* {@inheritdoc}
*
* @return null|string
*/
public function getBlockPrefix() {
return 'appbundle_vehicledocscollection';
}
}