Jacek Kowalski
4 hours ago 6b9278a5d57091fce636dbb640b380cfb990db6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
 
namespace App\Form;
 
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
 
class BulkOustType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options): void {
        $confirm = $options['confirm'];
 
        $builder
            ->add('notSeenSince', DateType::class, [
                'label' => 'bulk_oust_not_seen_since_header',
                'widget' => 'single_text',
                'input' => 'datetime_immutable',
                'constraints' => [new NotNull()],
            ])
            ->add('reason', TextType::class, [
                'label' => 'ousted_reason_header',
                'constraints' => [new NotBlank(), new Length(max: 255)],
            ]);
 
        if ($confirm) {
            $builder
                ->add('selection', HiddenType::class, ['constraints' => [new NotBlank()]])
                ->add('save', SubmitType::class, ['label' => 'bulk_oust_action_oust', 'attr' => ['class' => 'btn-error']]);
        } else {
            $builder->add('preview', SubmitType::class, ['label' => 'bulk_oust_action_preview']);
        }
    }
 
    public function configureOptions(OptionsResolver $resolver): void {
        $resolver->setDefaults(['confirm' => false]);
        $resolver->setAllowedTypes('confirm', 'bool');
        $resolver->setNormalizer('method', static fn($options): string => $options['confirm'] ? 'POST' : 'GET');
        $resolver->setNormalizer('csrf_protection', static fn($options, $value): bool => $options['confirm'] && $value);
    }
}