src/Form/ContactUsFormType.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\FormBuilderInterface;
  5. use Symfony\Component\Validator\Constraints\Email;
  6. use Symfony\Component\Validator\Constraints\Regex;
  7. use Symfony\Component\Validator\Constraints\NotBlank;
  8. use Symfony\Component\OptionsResolver\OptionsResolver;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use Symfony\Component\Form\Extension\Core\Type\TextType;
  11. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  12. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  13. use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
  14. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  15. use Pimcore\Localization\LocaleServiceInterface;
  16. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  17. use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue as RecaptchaTrue;
  18. class ContactUsFormType extends AbstractType
  19. {
  20.     
  21.     public function buildForm(FormBuilderInterface $builder, array $options)
  22.     {
  23.            $countries \Pimcore::getContainer()->get(LocaleServiceInterface::class)->getDisplayRegions();
  24.         asort($countries);
  25.        
  26.         $builder
  27.             ->add('firstname'TextType::class, [
  28.                 'label' => false,
  29.                 'constraints' => [
  30.                     new NotBlank([
  31.                         'message' => $options['translator']->trans('constraint.name')
  32.                     ]),
  33.                 ],
  34.             ])
  35.             ->add('lastname'TextType::class, [
  36.                 'label' => false,
  37.                 'constraints' => [
  38.                     new NotBlank([
  39.                         'message' => $options['translator']->trans('constraint.name')
  40.                     ]),
  41.                 ],
  42.             ])
  43.             ->add('number'NumberType::class, [
  44.                 'label' => false,
  45.                 // 'attr' => [
  46.                 //     'inputmode' => 'numeric',
  47.                 // ],
  48.                 'constraints' => [
  49.                     new NotBlank([
  50.                         'message' =>  $options['translator']->trans('constraint.phone'),
  51.                     ]),
  52.                     new Regex([
  53.                         'pattern' => '/^\d+$/',
  54.                         'message' =>  $options['translator']->trans('constraint.valid-phone'),
  55.                     ]),
  56.                 ],
  57.             ])
  58.             
  59.             ->add('email'EmailType::class, [
  60.                 'label' => false,
  61.                 'constraints' => [
  62.                     new NotBlank([
  63.                         'message' =>  $options['translator']->trans('constraint.email'),
  64.                     ]),
  65.                     new Email([
  66.                         'message' => $options['translator']->trans('constraint.valid-email'),
  67.                     ]),
  68.                 ],
  69.             ])
  70.             
  71.            
  72.             ->add('country'ChoiceType::class, [
  73.                 'choices' => $this->getCountryChoices($countries),
  74.                 'label' => false,
  75.                 'constraints' => [
  76.                     new NotBlank([
  77.                         'message' => 'Please Select Country',
  78.                     ]),
  79.                 ],
  80.                 'placeholder' => 'Country*'// Add a default option
  81.                 'required' => true,
  82.             ])
  83.             ->add('nationality'ChoiceType::class, [
  84.                 'choices' => $this->getCountryChoices($countries),
  85.                 'label' => false,
  86.                 'constraints' => [
  87.                     new NotBlank([
  88.                         'message' => 'Please Select Country',
  89.                     ]),
  90.                 ],
  91.                 'placeholder' => 'Nationality*'// Add a default option
  92.                 'required' => true// Make the field required
  93.             ])
  94.           
  95.             ->add('comments'TextareaType::class, [
  96.                 'label' => false,
  97.                 'constraints' => [
  98.                     new NotBlank([
  99.                         'message' =>  $options['translator']->trans('constraint.message')
  100.                     ]),
  101.                 ],
  102.                 'required' => true
  103.             ]);
  104.             
  105.     }
  106.     public function configureOptions(OptionsResolver $resolver)
  107.     {
  108.         $resolver->setRequired('translator');
  109.         $resolver->setAllowedTypes('translator', [TranslatorInterface::class, LocaleAwareInterface::class]);
  110.         $resolver->setDefaults([
  111.             // Configure your form options here...
  112.         ]);
  113.     }
  114.     private function getCountryChoices(array $countries)
  115. {
  116.     $choices = [];
  117.     foreach ($countries as $name) {
  118.         $choices[$name] = $name;
  119.     }
  120.     return $choices;
  121. }
  122. }