src/Form/Main/ContactType.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Form\Main;
  3. use App\Services\TranslationManager;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  6. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  7. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  8. use Symfony\Component\Form\Extension\Core\Type\TextType;
  9. use Symfony\Component\Form\FormBuilderInterface;
  10. use Symfony\Component\OptionsResolver\OptionsResolver;
  11. use Symfony\Component\Validator\Constraints\Email;
  12. use Symfony\Component\Validator\Constraints\NotBlank;
  13. class ContactType extends AbstractType
  14. {
  15.     protected $recaptchaKey;
  16.     protected $recaptchaTestKey;
  17.     private $environment;
  18.     public function __construct($recaptchaKey$recaptchaTestKey$environment)
  19.     {
  20.         $this->recaptchaKey $recaptchaKey;
  21.         $this->recaptchaTestKey $recaptchaTestKey;
  22.         $this->environment $environment;
  23.     }
  24.     public function buildForm(FormBuilderInterface $builder, array $options)
  25.     {
  26.         if ('test' == $this->environment) {
  27.             $recaptchaKey $this->recaptchaTestKey;
  28.         } else {
  29.             $recaptchaKey $this->recaptchaKey;
  30.         }
  31.         $builder
  32.             ->add(
  33.                 'firstName',
  34.                 TextType::class,
  35.                 [
  36.                     'constraints' => [
  37.                         new NotBlank()
  38.                     ]
  39.                 ]
  40.             )
  41.             ->add(
  42.                 'lastName',
  43.                 TextType::class,
  44.                 [
  45.                     'constraints' => [
  46.                         new NotBlank()
  47.                     ]
  48.                 ]
  49.             )
  50.             ->add(
  51.                 'email',
  52.                 EmailType::class,
  53.                 [
  54.                     'constraints' => [
  55.                         new Email(),
  56.                         new NotBlank()
  57.                     ]
  58.                 ]
  59.             )
  60.             ->add('orderId')
  61.             ->add(
  62.                 'title',
  63.                 TextType::class,
  64.                 [
  65.                     'constraints' => [
  66.                         new NotBlank()
  67.                     ]
  68.                 ]
  69.             )
  70.             ->add(
  71.                 'message',
  72.                 TextareaType::class,
  73.                 [
  74.                     'constraints' => [
  75.                         new NotBlank()
  76.                     ]
  77.                 ]
  78.             )
  79.             ->add(
  80.                 'submit',
  81.                 SubmitType::class,
  82.                 [
  83.                     "attr" => [
  84.                         "class" => "g-recaptcha btn btn-primary btn-lg",
  85.                         "id" => "contact-form-submit-btn",
  86.                         "data-sitekey" => $recaptchaKey,
  87.                         "data-callback" => "onSubmit"
  88.                      ]
  89.                 ]
  90.             )
  91.            ;
  92.     }
  93.     /**
  94.      * {@inheritdoc}
  95.      */
  96.     public function configureOptions(OptionsResolver $resolver)
  97.     {
  98.         $resolver->setDefaults(['translation_domain' => TranslationManager::TRANSLATION_DOMAIN_SITE]);
  99.     }
  100. }