<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Constraints as Assert;
class NewsletterSubscriptionType extends AbstractType
{
/**
* @var Security
*/
private Security $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$email = null;
if ($user = $this->security->getUser()) {
$email = $user->getUsername();
}
$builder->add(
'email',
EmailType::class,
[
'attr' => [
'value' => $email,
'placeholder' => 'exemple@gmail.com'
],
'constraints' => [
new Assert\NotBlank()
]
]
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => null
]);
}
}