From: Raphaƫl Gertz Date: Wed, 11 Dec 2019 03:59:23 +0000 (+0100) Subject: Add constructor to have doctrine and translator instance X-Git-Url: https://git.rapsys.eu/airbundle/commitdiff_plain/0b3fc5c5ebcb1c16e82852cc37ec9bd34ceb62cf Add constructor to have doctrine and translator instance Add admin, return, user and slot options Add user select from a custom tree for admin users Switch to html5 date picker Add return hidden field --- diff --git a/Form/ApplicationType.php b/Form/ApplicationType.php index 8439d29..1107ac6 100644 --- a/Form/ApplicationType.php +++ b/Form/ApplicationType.php @@ -6,28 +6,64 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; 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; 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) { - return $builder - ->add('location', EntityType::class, array('class' => 'RapsysAirBundle:Location', 'choice_label' => 'title', 'attr' => array('placeholder' => 'Your location'), 'constraints' => array(new NotBlank(array('message' => 'Please provide your location'))))) - ->add('date', DateType::class, array('attr' => [ 'placeholder' => 'Your date', 'class' => 'date' ], 'html5' => true, 'input' => 'datetime', 'data' => new \DateTime('+7 day'), 'constraints' => array(new NotBlank(array('message' => 'Please provide your date')), new Date(array('message' => 'Your date doesn\'t seems to be valid'))))) - ->add('slot', EntityType::class, array('class' => 'RapsysAirBundle:Slot', 'choice_label' => 'title', 'attr' => array('placeholder' => 'Your slot'), 'constraints' => array(new NotBlank(array('message' => 'Please provide your slot'))))) - ->add('submit', SubmitType::class, array('label' => 'Send', 'attr' => array('class' => 'submit'))); + //Retrieve translated slot + #$slots = $this->doctrine->getRepository(Slot::class)->findAllWithTranslatedTitle($this->translator); + + //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'])]]) + ->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('return', HiddenType::class, ['data' => $options['return']]) + ->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]); + + //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]); + } + + //Return form + return $form; } /** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { - $resolver->setDefaults(['error_bubbling' => true]); + $resolver->setDefaults(['error_bubbling' => true, 'admin' => false, 'return' => '', 'slot' => 3, 'user' => 0]); + $resolver->setAllowedTypes('admin', 'boolean'); + $resolver->setAllowedTypes('return', 'string'); + $resolver->setAllowedTypes('slot', Slot::class); + $resolver->setAllowedTypes('user', 'integer'); } /**