]> Raphaël G. Git Repositories - airbundle/blobdiff - Form/ApplicationType.php
Add city, country and zipcode
[airbundle] / Form / ApplicationType.php
index 8778fe9ff43f25abf8f139a4047efd29b62d182d..9d8dc83891a148cd1df1dce8c0acf80b3cd7c1c3 100644 (file)
@@ -1,4 +1,13 @@
-<?php
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys AirBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
 
 namespace Rapsys\AirBundle\Form;
 
@@ -8,49 +17,46 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
 use Symfony\Bridge\Doctrine\Form\Type\EntityType;
 use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
 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\Validator\Constraints\Date;
 use Symfony\Component\Validator\Constraints\NotBlank;
-use Symfony\Bridge\Doctrine\RegistryInterface;
-use Symfony\Component\Translation\TranslatorInterface;
-use Rapsys\AirBundle\Entity\User;
-use Rapsys\AirBundle\Entity\Slot;
+
+use Rapsys\AirBundle\Entity\Dance;
 use Rapsys\AirBundle\Entity\Location;
+use Rapsys\AirBundle\Entity\Slot;
 
+/**
+ * {@inheritdoc}
+ */
 class ApplicationType extends AbstractType {
-       //Doctrine instance
-       private $doctrine;
-
-       //Translator instance
-       protected $translator;
-
-       public function __construct(RegistryInterface $doctrine, TranslatorInterface $translator) {
-               $this->doctrine = $doctrine;
-               $this->translator = $translator;
-       }
-
        /**
         * {@inheritdoc}
         */
        public function buildForm(FormBuilderInterface $builder, array $options) {
-               //Retrieve translated slot
-               #$slots = $this->doctrine->getRepository(Slot::class)->findAllWithTranslatedTitle($this->translator);
+               //Create form
+               $form = $builder;
+
+               //Add dance field
+               $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']]);
+
+               //Add date field
+               $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'])]]);
+
+               //Add location field
+               $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']]);
 
-               //Create base form
-               $form = $builder
-                       ->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']])
-                       ->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'])]])
-                       #->add('slot', ChoiceType::class, ['attr' => ['placeholder' => 'Your slot'], 'constraints' => [new NotBlank(['message' => 'Please provide your slot'])], 'choices' => $slots, 'data' => $options['slot']])
-                       ->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']])
-                       ->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
+               //Add slot field
+               $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']]);
 
                //Add extra user field
-               if (!empty($options['admin'])) {
-                       $users = $this->doctrine->getRepository(User::class)->findAllWithTranslatedGroupAndTitle($this->translator);
-                       $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]);
+               if (!empty($options['user'])) {
+                       //XXX: choicetype used here to use our own custom translated string
+                       $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']]);
                }
 
+               //Add submit
+               $form->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
+
                //Return form
                return $form;
        }
@@ -59,12 +65,38 @@ class ApplicationType extends AbstractType {
         * {@inheritdoc}
         */
        public function configureOptions(OptionsResolver $resolver) {
-               //XXX: 1 should be the first user
-               $resolver->setDefaults(['error_bubbling' => true, 'admin' => false, 'slot' => null, 'location' => null, 'user' => 1]);
-               $resolver->setAllowedTypes('admin', 'boolean');
-               $resolver->setAllowedTypes('location', [Location::class, 'null']);
-               $resolver->setAllowedTypes('slot', [Slot::class, 'null']);
-               $resolver->setAllowedTypes('user', 'integer');
+               //Set defaults
+               $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]);
+
+               //Add dance choices
+               $resolver->setAllowedTypes('dance_choices', 'array');
+
+               //Add dance default
+               $resolver->setAllowedTypes('dance_default', [Dance::class, 'null']);
+
+               //Add dance favorites
+               $resolver->setAllowedTypes('dance_favorites', 'array');
+
+               //Add location choices
+               $resolver->setAllowedTypes('location_choices', 'array');
+
+               //Add location default
+               $resolver->setAllowedTypes('location_default', [Location::class, 'null']);
+
+               //Add location favorites
+               $resolver->setAllowedTypes('location_favorites', 'array');
+
+               //Add slot default
+               $resolver->setAllowedTypes('slot_default', [Slot::class, 'null']);
+
+               //Add user field
+               $resolver->setAllowedTypes('user', 'boolean');
+
+               //Add user choices
+               $resolver->setAllowedTypes('user_choices', 'array');
+
+               //Add user default
+               $resolver->setAllowedTypes('user_default', 'integer');
        }
 
        /**