<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use App\Form\NoticePodnetType;
use App\Form\NoticeOznamenieType;
use App\Entity\Notice;
use App\Form\LocType;
use App\Form\DocType;
class NoticeType extends AbstractType {
private $options;
public function buildForm(FormBuilderInterface $builder, array $options) {
$this->options = $options;
$builder
->add('nazov', TextType::class)
->add('locs', CollectionType::class, [
'entry_type' => LocType::class,
'entry_options' => [
'ciselnikObci' => $options['ciselnikObci'],
],
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'required' => false,
'label' => false,
])
->add('fileupload', FileType::class,[
'mapped' => false,
'required' => false,
'label' => false,
])
->add('docs', CollectionType::class, [
'entry_type' => DocType::class,
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'required' => false,
'label' => false,
])
->add('operators', CollectionType::class, [
'entry_type' => OperatorType::class,
'entry_options' => [
'ciselniky' => $options['ciselniky'],
],
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'required' => false,
'label' => false,
])
// ->add('gdpr', CheckboxType::class, [
// 'label' => false,
// 'required' => true,
// 'mapped' => false,
// ])
->addEventListener(
FormEvents::PRE_SET_DATA,
[$this, 'onPreSubmit']
)
//->getForm()
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => Notice::class,
'ciselniky' => null,
'ciselnikObci' => null,
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix() {
return 'notice';
}
public function onPreSubmit(FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
if ($data->getType() == 'oznamenie') {
$form->add('oznamenie', NoticeOznamenieType::class, [
'label' => false,
'ciselniky' => $this->options['ciselniky'],
]);
} else {
$form->add('nazov', TextType::class, [
'label' => 'Názov podnetu:',
'attr' => [
'class' => 'form-control',
'placeholder' => 'Názov podnetu...',
],
])
->add('podnet', NoticePodnetType::class, [
'label' => false,
'ciselniky' => $this->options['ciselniky'],
]);
}
}
}