src/Form/VehicleRegistrationHasDoc.php line 13
<?php
namespace App\Form;
use App\Entity\VehicleDocs;
use App\Entity\VehicleRegistrations;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
class VehicleRegistrationHasDoc extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$locale = $options['locale'];
$formModifier = function ($form) use($locale) {
$form->add('registrationDoc', VehicleDocsCollectionType::class, array(
'label' => false,
'data_class' => VehicleDocs::class,
'by_reference' => false,
'locale' => $locale
));
};
//this will add registrationDoc field to the form before fetching form fields data from DB
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($formModifier) {
// this is the entity, i.e. registrationDoc
$form= $event->getForm();
$formModifier($form);
});
//this will add registrationDoc field to the form after the from is posted and before mapping the posted data to the FormType class
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($formModifier) {
// this is the posted data as an array
$child = $event->getData();
$form = $event->getForm();
if (isset($child['registrationDoc'])) {
$formModifier($form);
}
});
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => VehicleRegistrations::class,
'translation_domain' => 'vehicles',
'locale' => 'en'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_vehicleregistrationsdoc';
}
}