]> Raphaël G. Git Repositories - airbundle/blob - Form/DanceType.php
Version 0.5.1
[airbundle] / Form / DanceType.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys AirBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\AirBundle\Form;
13
14 use Doctrine\ORM\EntityManagerInterface;
15
16 use Symfony\Component\Form\AbstractType;
17 use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
18 use Symfony\Component\Form\Extension\Core\Type\SubmitType;
19 use Symfony\Component\Form\FormBuilderInterface;
20 use Symfony\Component\OptionsResolver\OptionsResolver;
21 use Symfony\Contracts\Translation\TranslatorInterface;
22
23 use Rapsys\AirBundle\Transformer\DanceTransformer;
24
25 /**
26 * {@inheritdoc}
27 */
28 class DanceType extends AbstractType {
29 /**
30 * Constructor
31 *
32 * @param EntityManagerInterface $manager The entity manager
33 * @param ?TranslatorInterface $translator The translator instance
34 */
35 public function __construct(protected EntityManagerInterface $manager, protected ?TranslatorInterface $translator = null) {
36 }
37
38 /**
39 * {@inheritdoc}
40 */
41 public function buildForm(FormBuilderInterface $builder, array $options): void {
42 $builder->add(
43 $builder
44 ->create('dances', ChoiceType::class, ['attr' => ['placeholder' => 'Your dance']/*, 'by_reference' => false*/, 'choice_attr' => ['class' => 'row'], 'choice_translation_domain' => false, 'choices' => $options['dance_choices'], 'multiple' => true, 'preferred_choices' => $options['dance_favorites'], 'required' => false])
45 ->addModelTransformer(new DanceTransformer($this->manager))
46 );
47
48 //Add submit
49 $builder->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
50 }
51
52 /**
53 * {@inheritdoc}
54 */
55 public function configureOptions(OptionsResolver $resolver): void {
56 //Set defaults
57 $resolver->setDefaults(['error_bubbling' => true, 'dance_choices' => [], 'dance_default' => null, 'dance_favorites' => []]);
58
59 //Add dance choices
60 $resolver->setAllowedTypes('dance_choices', 'array');
61
62 //Add dance default
63 $resolver->setAllowedTypes('dance_default', [Dance::class, 'null']);
64
65 //Add dance favorites
66 $resolver->setAllowedTypes('dance_favorites', 'array');
67
68 }
69
70 /**
71 * {@inheritdoc}
72 */
73 public function getName(): string {
74 return 'rapsysair_application';
75 }
76 }