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 base form $form = $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 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 extra user field if (!empty($options['admin'])) { $users = $this->doctrine->getRepository(User::class)->findAllWithTranslatedGroupAndCivility($this->translator); $form ->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('user', ChoiceType::class, ['attr' => ['placeholder' => 'Your user'], 'choice_translation_domain' => false, 'constraints' => [new NotBlank(['message' => 'Please provide your user'])], 'choices' => $users, 'data' => $options['user']]); //Add user locations } else { $locations = $this->doctrine->getRepository(Location::class)->findByUserId($options['user']); $form->add('location', EntityType::class, ['class' => 'RapsysAirBundle:Location', 'choices' => $locations, 'attr' => ['placeholder' => 'Your location'], 'choice_translation_domain' => true, 'constraints' => [new NotBlank(['message' => 'Please provide your location'])], 'data' => $options['location']]); } //Return form return $form; } /** * {@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'); } /** * {@inheritdoc} */ public function getName() { return 'rapsys_air_application'; } }