src/Form/SubscriptionType.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Subscription;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  6. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  7. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  8. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  9. use Symfony\Component\Form\Extension\Core\Type\TextType;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Symfony\Component\OptionsResolver\OptionsResolver;
  12. use Symfony\Component\Validator\Constraints\Email;
  13. use Symfony\Component\Validator\Constraints\NotBlank;
  14. class SubscriptionType extends AbstractType
  15. {
  16.     public function __construct(private string $countriesJsonFile)
  17.     {}
  18.     public function buildForm(FormBuilderInterface $builder, array $options)
  19.     { 
  20.         $builder
  21.             ->add('countryCode'ChoiceType::class, [     
  22.                     'required' => true
  23.                     'choices' => $this->getCountriesOptions(),              
  24.                     'attr' => [
  25.                         'placeholder' => 'choose an option',
  26.                         'class'=>'form-control'
  27.                     ],
  28.                     /*
  29.                     'choice_label' => function($item){
  30.                         $data = (array) $item;
  31.                         return sprintf('%s %s', $data['dial_code'], $data['name']);
  32.                     },
  33.                     */
  34.                     'choice_value' => function($item){
  35.                         
  36.                         return $item;         
  37.                     } ,
  38.                     'constraints' => [
  39.                         new NotBlank()
  40.                     ],
  41.                     'data'=>'United States'
  42.                 ]
  43.             )
  44.             ->add('phoneNumber'TextType::class, [  
  45.                     'required' => true,
  46.                     'attr' => [
  47.                         'placeholder' => 'Phone',
  48.                         'class'=>'form-control'
  49.                     ],
  50.                     'constraints' => [
  51.                         new NotBlank()
  52.                     ],                    
  53.                 ]
  54.             ) 
  55.             ->add('email'EmailType::class, [                    
  56.                 'attr' => [
  57.                     'placeholder' => 'Enter your email',
  58.                     'class'=> 'form-control mb-3'
  59.                 ],
  60.                 'constraints' => [
  61.                     new Email(['message' => 'Invalid email address.'])
  62.                 ],
  63.              ])
  64.             ->add('optIn'CheckboxType::class, [                    
  65.                 'required'=>false,
  66.                 'label' => 'Keep me up to date on news and offers',
  67.                 'attr' => [
  68.                     'class' => 'form-check-input'
  69.                 ]
  70.             ])
  71.             ->add('submit'SubmitType::class, [
  72.                 'label' => 'Subscribe Now',
  73.                 'attr' => [
  74.                     'class'=>'btn btn-primary btn-primary-fast'
  75.                 ]
  76.             ])
  77.         ;     
  78.     }  
  79.     
  80.     private function getCountriesOptions(): array
  81.     {
  82.         $countries json_decode(file_get_contents($this->countriesJsonFiletrue));
  83.         $data = [];
  84.         foreach($countries as $country){
  85.             $country = (array)$country;
  86.             $data[$country['dial_code'].' '.$country['name']] = $country['name'];
  87.         }
  88.         return $data;
  89.     }
  90.     public function configureOptions(OptionsResolver $resolver): void
  91.     {
  92.         $resolver->setDefaults([
  93.             'data_class' => Subscription::class
  94.         ]);
  95.     }
  96. }