src/Form/ContactUsFormType.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\CheckboxType;
  5. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  6. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  7. use Symfony\Component\Form\Extension\Core\Type\TextType;
  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 ContactUsFormType extends AbstractType
  14. {
  15.     private TranslatorInterface $translator;
  16.     public function __construct(TranslatorInterface $translator)
  17.     {
  18.         $this->translator $translator;
  19.     }
  20.     /**
  21.      * @param FormBuilderInterface $builder
  22.      * @param array $options
  23.      */
  24.     public function buildForm(FormBuilderInterface $builder, array $options): void
  25.     {
  26.         $builder
  27.             ->add('name'TextType::class, [
  28.                 'label' => $this->translator->trans('contact-us.name.label'),
  29.                 //'required' => false // this is overridden on frontend - { attr: { 'novalidate': 'novalidate'}}
  30.                 'translation_domain' => false,
  31.                 'attr' => [
  32.                     'required' => false
  33.                 ],
  34.                 'constraints' => [
  35.                     new Length([
  36.                         'max' => 100,
  37.                         'maxMessage' => $this->translator->trans('contact-us.name.max')
  38.                     ])
  39.                 ]
  40.             ])
  41.             ->add('email'TextType::class, [
  42.                 'label' => $this->translator->trans('contact-us.email.label'),
  43.                 'translation_domain' => false,
  44.                 'attr' => [
  45.                     'required' => true
  46.                 ],
  47.                 'constraints' => [
  48.                     new NotBlank([
  49.                         'message' => $this->translator->trans('contact-us.email.required')
  50.                     ]),
  51.                     new Email([
  52.                         'message' => $this->translator->trans('contact-us.email.valid')
  53.                     ])
  54.                 ]
  55.             ])
  56.             ->add('message'TextareaType::class, [
  57.                 'label' => $this->translator->trans('contact-us.message.label'),
  58.                 'translation_domain' => false,
  59.                 'attr' => [
  60.                     'required' => true
  61.                 ],
  62.                 'constraints' => [
  63.                     new NotBlank([
  64.                         'message' => $this->translator->trans('meddox.message.message.blank')
  65.                     ])
  66.                 ]
  67.             ])
  68.             ->add('privacyConsent'CheckboxType::class, [
  69.                 'label' => 'contact-us.privacy-consent.label',
  70.                 'attr' => [
  71.                     'class' => 'privacyConsent',
  72.                     'required' => false
  73.                 ]
  74.             ])
  75.             ->add('promotiveMaterialConsent'CheckboxType::class, [
  76.                 'label' => $this->translator->trans('contact-us.promotive-material-consent.label'),
  77.                 'translation_domain' => false,
  78.                 'required' => false,
  79.                 'attr' => [
  80.                     'class' => 'promotiveMaterialConsent',
  81.                 ]
  82.             ])
  83.             ->add('submit'SubmitType::class, [
  84.                 'label' => $this->translator->trans('meddox.submit.label')
  85.             ]);
  86.     }
  87. }