<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Pimcore\Localization\LocaleServiceInterface;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue as RecaptchaTrue;
class ContactUsFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$countries = \Pimcore::getContainer()->get(LocaleServiceInterface::class)->getDisplayRegions();
asort($countries);
$builder
->add('firstname', TextType::class, [
'label' => false,
'constraints' => [
new NotBlank([
'message' => $options['translator']->trans('constraint.name')
]),
],
])
->add('lastname', TextType::class, [
'label' => false,
'constraints' => [
new NotBlank([
'message' => $options['translator']->trans('constraint.name')
]),
],
])
->add('number', NumberType::class, [
'label' => false,
// 'attr' => [
// 'inputmode' => 'numeric',
// ],
'constraints' => [
new NotBlank([
'message' => $options['translator']->trans('constraint.phone'),
]),
new Regex([
'pattern' => '/^\d+$/',
'message' => $options['translator']->trans('constraint.valid-phone'),
]),
],
])
->add('email', EmailType::class, [
'label' => false,
'constraints' => [
new NotBlank([
'message' => $options['translator']->trans('constraint.email'),
]),
new Email([
'message' => $options['translator']->trans('constraint.valid-email'),
]),
],
])
->add('country', ChoiceType::class, [
'choices' => $this->getCountryChoices($countries),
'label' => false,
'constraints' => [
new NotBlank([
'message' => 'Please Select Country',
]),
],
'placeholder' => 'Country*', // Add a default option
'required' => true,
])
->add('nationality', ChoiceType::class, [
'choices' => $this->getCountryChoices($countries),
'label' => false,
'constraints' => [
new NotBlank([
'message' => 'Please Select Country',
]),
],
'placeholder' => 'Nationality*', // Add a default option
'required' => true, // Make the field required
])
->add('comments', TextareaType::class, [
'label' => false,
'constraints' => [
new NotBlank([
'message' => $options['translator']->trans('constraint.message')
]),
],
'required' => true
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired('translator');
$resolver->setAllowedTypes('translator', [TranslatorInterface::class, LocaleAwareInterface::class]);
$resolver->setDefaults([
// Configure your form options here...
]);
}
private function getCountryChoices(array $countries)
{
$choices = [];
foreach ($countries as $name) {
$choices[$name] = $name;
}
return $choices;
}
}