]> Raphaƫl G. Git Repositories - airbundle/blob - Form/ApplicationType.php
Add snippet feature
[airbundle] / Form / ApplicationType.php
1 <?php
2
3 namespace Rapsys\AirBundle\Form;
4
5 use Symfony\Component\Form\AbstractType;
6 use Symfony\Component\Form\FormBuilderInterface;
7 use Symfony\Component\OptionsResolver\OptionsResolver;
8 use Symfony\Bridge\Doctrine\Form\Type\EntityType;
9 use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
10 use Symfony\Component\Form\Extension\Core\Type\DateType;
11 use Symfony\Component\Form\Extension\Core\Type\HiddenType;
12 use Symfony\Component\Form\Extension\Core\Type\SubmitType;
13 use Symfony\Component\Validator\Constraints\Date;
14 use Symfony\Component\Validator\Constraints\NotBlank;
15 use Symfony\Bridge\Doctrine\RegistryInterface;
16 use Symfony\Component\Translation\TranslatorInterface;
17 use Rapsys\AirBundle\Entity\User;
18 use Rapsys\AirBundle\Entity\Slot;
19 use Rapsys\AirBundle\Entity\Location;
20
21 class ApplicationType extends AbstractType {
22 //Doctrine instance
23 private $doctrine;
24
25 //Translator instance
26 protected $translator;
27
28 public function __construct(RegistryInterface $doctrine, TranslatorInterface $translator) {
29 $this->doctrine = $doctrine;
30 $this->translator = $translator;
31 }
32
33 /**
34 * {@inheritdoc}
35 */
36 public function buildForm(FormBuilderInterface $builder, array $options) {
37 //Retrieve translated slot
38 #$slots = $this->doctrine->getRepository(Slot::class)->findAllWithTranslatedTitle($this->translator);
39
40 //Create base form
41 $form = $builder
42 ->add('location', EntityType::class, ['class' => 'RapsysAirBundle:Location', 'attr' => ['placeholder' => 'Your location'], 'choice_translation_domain' => true, 'constraints' => [new NotBlank(['message' => 'Please provide your location'])], 'data' => $options['location']])
43 ->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 #->add('slot', ChoiceType::class, ['attr' => ['placeholder' => 'Your slot'], 'constraints' => [new NotBlank(['message' => 'Please provide your slot'])], 'choices' => $slots, 'data' => $options['slot']])
45 ->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']])
46 ->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
47
48 //Add extra user field
49 if (!empty($options['admin'])) {
50 $users = $this->doctrine->getRepository(User::class)->findAllWithTranslatedGroupAndTitle($this->translator);
51 $form->add('user', ChoiceType::class, ['attr' => ['placeholder' => 'Your user'], 'constraints' => [new NotBlank(['message' => 'Please provide your user'])], 'choices' => $users, 'data' => $options['user'], 'choice_translation_domain' => false]);
52 }
53
54 //Return form
55 return $form;
56 }
57
58 /**
59 * {@inheritdoc}
60 */
61 public function configureOptions(OptionsResolver $resolver) {
62 //XXX: 1 should be the first user
63 $resolver->setDefaults(['error_bubbling' => true, 'admin' => false, 'slot' => null, 'location' => null, 'user' => 1]);
64 $resolver->setAllowedTypes('admin', 'boolean');
65 $resolver->setAllowedTypes('location', [Location::class, 'null']);
66 $resolver->setAllowedTypes('slot', [Slot::class, 'null']);
67 $resolver->setAllowedTypes('user', 'integer');
68 }
69
70 /**
71 * {@inheritdoc}
72 */
73 public function getName() {
74 return 'rapsys_air_application';
75 }
76 }