src/Form/NewsletterSubscriptionType.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  5. use Symfony\Component\Form\FormBuilderInterface;
  6. use Symfony\Component\OptionsResolver\OptionsResolver;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. class NewsletterSubscriptionType extends AbstractType
  10. {
  11.     /**
  12.      * @var Security
  13.      */
  14.     private Security $security;
  15.     public function __construct(Security $security)
  16.     {
  17.         $this->security $security;
  18.     }
  19.     public function buildForm(FormBuilderInterface $builder, array $options)
  20.     {
  21.         $email null;
  22.         if ($user $this->security->getUser()) {
  23.             $email $user->getUsername();
  24.         }
  25.         $builder->add(
  26.             'email',
  27.             EmailType::class,
  28.             [
  29.                 'attr' => [
  30.                     'value' => $email,
  31.                     'placeholder' => 'exemple@gmail.com'
  32.                 ],
  33.                 'constraints' => [
  34.                     new Assert\NotBlank()
  35.                 ]
  36.             ]
  37.         );
  38.     }
  39.     public function configureOptions(OptionsResolver $resolver)
  40.     {
  41.         $resolver->setDefaults([
  42.             'data_class' => null
  43.         ]);
  44.     }
  45. }