<?php declare(strict_types=1);
namespace App\Form\Type;
use App\Entity\ContentReport;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ContentReportType extends AbstractType
{
/**
* @param array<mixed> $options
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('url', UrlType::class, [
'label' => 'form.url',
])
->add('type', ChoiceType::class, [
'label' => 'form.type',
'choices' => ContentReport::$typeOfInfringement,
])
->add('text', TextareaType::class, [
'label' => 'form.text',
])
->add('signature', TextType::class, [
'label' => 'form.signature',
])
->add('reporter', ContentReportDataType::class, [
'label' => 'form.reporter',
'by_reference' => false,
])
->add('reporterIsVictim', CheckboxType::class, [
'label' => 'form.reporter_is_victim',
'required' => false,
'label_attr' => [
'class' => 'checkbox-custom',
],
])
->add('victim', ContentReportDataType::class, [
'label' => 'form.victim',
'required' => false,
'by_reference' => false,
])
->add('authorized', CheckboxType::class, [
'label' => false,
'required' => true,
])
;
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void {
/** @var array<mixed> $data */
$data = $event->getData();
if (isset($data['reporterIsVictim']) && $data['reporterIsVictim'] !== '') {
$data['victim'] = $data['reporter'];
}
$event->setData($data);
});
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'translation_domain' => 'content_report',
'data_class' => ContentReport::class,
]);
}
}