<?php
namespace App\Form\Main;
use App\Services\TranslationManager;
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\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
class ContactType extends AbstractType
{
protected $recaptchaKey;
protected $recaptchaTestKey;
private $environment;
public function __construct($recaptchaKey, $recaptchaTestKey, $environment)
{
$this->recaptchaKey = $recaptchaKey;
$this->recaptchaTestKey = $recaptchaTestKey;
$this->environment = $environment;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
if ('test' == $this->environment) {
$recaptchaKey = $this->recaptchaTestKey;
} else {
$recaptchaKey = $this->recaptchaKey;
}
$builder
->add(
'firstName',
TextType::class,
[
'constraints' => [
new NotBlank()
]
]
)
->add(
'lastName',
TextType::class,
[
'constraints' => [
new NotBlank()
]
]
)
->add(
'email',
EmailType::class,
[
'constraints' => [
new Email(),
new NotBlank()
]
]
)
->add('orderId')
->add(
'title',
TextType::class,
[
'constraints' => [
new NotBlank()
]
]
)
->add(
'message',
TextareaType::class,
[
'constraints' => [
new NotBlank()
]
]
)
->add(
'submit',
SubmitType::class,
[
"attr" => [
"class" => "g-recaptcha btn btn-primary btn-lg",
"id" => "contact-form-submit-btn",
"data-sitekey" => $recaptchaKey,
"data-callback" => "onSubmit"
]
]
)
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['translation_domain' => TranslationManager::TRANSLATION_DOMAIN_SITE]);
}
}