src/Form/NewsletterFormType.php line 18

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\Extension\Core\Type\SubmitType;
  6. use Symfony\Component\Form\Extension\Core\Type\TextType;
  7. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  8. use Symfony\Component\Form\FormBuilderInterface;
  9. use Symfony\Component\Validator\Constraints\Email;
  10. use Symfony\Component\Validator\Constraints\Length;
  11. use Symfony\Component\Validator\Constraints\NotBlank;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. class NewsletterFormType extends AbstractType
  14. {
  15.     private $translator;
  16.     public function __construct(TranslatorInterface $translator)
  17.     {
  18.         $this->translator $translator;
  19.     }
  20.     public function buildForm(FormBuilderInterface $builder, array $options)
  21.     {
  22.         $builder
  23.             ->add('email'TextType::class, [
  24.                 'label' => $this->translator->trans('newsletter.email.label'),
  25.                 'attr' => [
  26.                     'required' => true
  27.                 ],
  28.                 'constraints' => [
  29.                     new NotBlank([
  30.                         'message' => $this->translator->trans('newsletter.email.required')
  31.                     ]),
  32.                     new Length([
  33.                         'max' => 100,
  34.                         'maxMessage' => $this->translator->trans('newsletter.email.max')
  35.                     ]),
  36.                     new Email([
  37.                         'message' => $this->translator->trans('newsletter.email.valid')
  38.                     ]),
  39.                 ]
  40.             ])
  41.             ->add('privacyConsent'CheckboxType::class, [
  42.                 'label' => 'newsletter.privacy-consent.label',
  43.                 'attr' => [
  44.                     'class' => 'privacyConsent',
  45.                     'required' => false
  46.                 ],
  47.                 'constraints' => [
  48.                     new NotBlank([
  49.                         'message' => $this->translator->trans('newsletter.privacy-consent.required')
  50.                     ])
  51.                 ]
  52.             ])
  53.             ->add('submit'SubmitType::class, [
  54.                 'label' => $this->translator->trans('newsletter.submit.label')
  55.             ]);
  56.     }
  57. }