]> Raphaël G. Git Repositories - airbundle/blob - Form/ApplicationType.php
Use controller inherited doctrine, manager, slugger members
[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\Date;
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) {
36 //Create form
37 $form = $builder;
38
39 //Add dance field
40 $form->add('dance', EntityType::class, ['class' => 'RapsysAirBundle: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'])], 'empty_data' => $options['dance_default']]);
41
42 //Add date field
43 $form->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 Date(['message' => 'Your date doesn\'t seems to be valid'])]]);
44
45 //Add location field
46 $form->add('location', EntityType::class, ['class' => 'RapsysAirBundle: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'])], 'empty_data' => $options['location_default']]);
47
48 //Add slot field
49 $form->add('slot', EntityType::class, ['class' => 'RapsysAirBundle:Slot', 'attr' => ['placeholder' => 'Your slot'], 'constraints' => [new NotBlank(['message' => 'Please provide your slot'])], 'choice_translation_domain' => true, 'data' => $options['slot_default']]);
50
51 //Add extra user field
52 if (!empty($options['user'])) {
53 //XXX: choicetype used here to use our own custom translated string
54 $form->add('user', ChoiceType::class, ['attr' => ['placeholder' => 'Your user'], 'choice_translation_domain' => false, 'constraints' => [new NotBlank(['message' => 'Please provide your user'])], 'choices' => $options['user_choices'], 'empty_data' => $options['user_default']]);
55 }
56
57 //Add submit
58 $form->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
59
60 //Return form
61 return $form;
62 }
63
64 /**
65 * {@inheritdoc}
66 */
67 public function configureOptions(OptionsResolver $resolver) {
68 //Set defaults
69 $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]);
70
71 //Add dance choices
72 $resolver->setAllowedTypes('dance_choices', 'array');
73
74 //Add dance default
75 $resolver->setAllowedTypes('dance_default', [Dance::class, 'null']);
76
77 //Add dance favorites
78 $resolver->setAllowedTypes('dance_favorites', 'array');
79
80 //Add location choices
81 $resolver->setAllowedTypes('location_choices', 'array');
82
83 //Add location default
84 $resolver->setAllowedTypes('location_default', [Location::class, 'null']);
85
86 //Add location favorites
87 $resolver->setAllowedTypes('location_favorites', 'array');
88
89 //Add slot default
90 $resolver->setAllowedTypes('slot_default', [Slot::class, 'null']);
91
92 //Add user field
93 $resolver->setAllowedTypes('user', 'boolean');
94
95 //Add user choices
96 $resolver->setAllowedTypes('user_choices', 'array');
97
98 //Add user default
99 $resolver->setAllowedTypes('user_default', 'integer');
100 }
101
102 /**
103 * {@inheritdoc}
104 */
105 public function getName() {
106 return 'rapsys_air_application';
107 }
108 }