]> Raphaël G. Git Repositories - airbundle/blob - Form/ApplicationType.php
Add created field to findAllIndexed results
[airbundle] / Form / ApplicationType.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 Symfony\Component\Form\AbstractType;
15 use Symfony\Component\Form\FormBuilderInterface;
16 use Symfony\Component\OptionsResolver\OptionsResolver;
17 use Symfony\Bridge\Doctrine\Form\Type\EntityType;
18 use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
19 use Symfony\Component\Form\Extension\Core\Type\DateType;
20 use Symfony\Component\Form\Extension\Core\Type\SubmitType;
21 use Symfony\Component\Validator\Constraints\Type;
22 use Symfony\Component\Validator\Constraints\NotBlank;
23
24 use Rapsys\AirBundle\Entity\Dance;
25 use Rapsys\AirBundle\Entity\Location;
26 use Rapsys\AirBundle\Entity\Slot;
27
28 /**
29 * {@inheritdoc}
30 */
31 class ApplicationType extends AbstractType {
32 /**
33 * {@inheritdoc}
34 */
35 public function buildForm(FormBuilderInterface $builder, array $options): void {
36 //Add dance field
37 $builder->add('dance', EntityType::class, ['class' => 'Rapsys\AirBundle\Entity\Dance', 'choices' => $options['dance_choices'], 'preferred_choices' => $options['dance_favorites'], 'attr' => ['placeholder' => 'Your dance'], 'choice_translation_domain' => true, 'constraints' => [new NotBlank(['message' => 'Please provide your dance'])], 'data' => $options['dance_default']]);
38
39 //Add date field
40 $builder->add('date', DateType::class, ['attr' => ['placeholder' => 'Your date', 'class' => 'date'], 'html5' => true, 'input' => 'datetime', 'widget' => 'single_text', 'format' => 'yyyy-MM-dd', 'data' => new \DateTime('+7 day'), 'constraints' => [new NotBlank(['message' => 'Please provide your date']), new Type(['type' => \DateTime::class, 'message' => 'Your date doesn\'t seems to be valid'])]]);
41
42 //Add location field
43 $builder->add('location', EntityType::class, ['class' => 'Rapsys\AirBundle\Entity\Location', 'choices' => $options['location_choices'], 'preferred_choices' => $options['location_favorites'], 'attr' => ['placeholder' => 'Your location'], 'choice_translation_domain' => true, 'constraints' => [new NotBlank(['message' => 'Please provide your location'])], 'data' => $options['location_default']]);
44
45 //Add slot field
46 $builder->add('slot', EntityType::class, ['class' => 'Rapsys\AirBundle\Entity\Slot', 'attr' => ['placeholder' => 'Your slot'], 'constraints' => [new NotBlank(['message' => 'Please provide your slot'])], 'choice_translation_domain' => true, 'data' => $options['slot_default']]);
47
48 //Add extra user field
49 if (!empty($options['user'])) {
50 //XXX: choicetype used here to use our own custom translated string
51 $builder->add('user', ChoiceType::class, ['attr' => ['placeholder' => 'Your user'], 'choice_translation_domain' => false, 'constraints' => [new NotBlank(['message' => 'Please provide your user'])], 'choices' => $options['user_choices'], 'data' => $options['user_default']]);
52 }
53
54 //Add submit
55 $builder->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
56 }
57
58 /**
59 * {@inheritdoc}
60 */
61 public function configureOptions(OptionsResolver $resolver): void {
62 //Set defaults
63 $resolver->setDefaults(['error_bubbling' => true, 'dance_choices' => [], 'dance_default' => null, 'dance_favorites' => [], 'location_choices' => [], 'location_default' => null, 'location_favorites' => [], 'slot_default' => null, 'user' => true, 'user_choices' => [], 'user_default' => 1]);
64
65 //Add dance choices
66 $resolver->setAllowedTypes('dance_choices', 'array');
67
68 //Add dance default
69 $resolver->setAllowedTypes('dance_default', [Dance::class, 'null']);
70
71 //Add dance favorites
72 $resolver->setAllowedTypes('dance_favorites', 'array');
73
74 //Add location choices
75 $resolver->setAllowedTypes('location_choices', 'array');
76
77 //Add location default
78 $resolver->setAllowedTypes('location_default', [Location::class, 'null']);
79
80 //Add location favorites
81 $resolver->setAllowedTypes('location_favorites', 'array');
82
83 //Add slot default
84 $resolver->setAllowedTypes('slot_default', [Slot::class, 'null']);
85
86 //Add user field
87 $resolver->setAllowedTypes('user', 'boolean');
88
89 //Add user choices
90 $resolver->setAllowedTypes('user_choices', 'array');
91
92 //Add user default
93 $resolver->setAllowedTypes('user_default', 'integer');
94 }
95
96 /**
97 * {@inheritdoc}
98 */
99 public function getName(): string {
100 return 'rapsysair_application';
101 }
102 }