<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Contracts\Translation\TranslatorInterface;
class NewsletterFormType extends AbstractType
{
private $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', TextType::class, [
'label' => $this->translator->trans('newsletter.email.label'),
'attr' => [
'required' => true
],
'constraints' => [
new NotBlank([
'message' => $this->translator->trans('newsletter.email.required')
]),
new Length([
'max' => 100,
'maxMessage' => $this->translator->trans('newsletter.email.max')
]),
new Email([
'message' => $this->translator->trans('newsletter.email.valid')
]),
]
])
->add('privacyConsent', CheckboxType::class, [
'label' => 'newsletter.privacy-consent.label',
'attr' => [
'class' => 'privacyConsent',
'required' => false
],
'constraints' => [
new NotBlank([
'message' => $this->translator->trans('newsletter.privacy-consent.required')
])
]
])
->add('submit', SubmitType::class, [
'label' => $this->translator->trans('newsletter.submit.label')
]);
}
}