src/Form/User/AddressType.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Form\User;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\Extension\Core\Type\CountryType;
  5. use Symfony\Component\Form\Extension\Core\Type\TextType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\Intl\Countries;
  8. use Symfony\Component\Validator\Constraints\NotBlank;
  9. class AddressType extends AbstractType
  10. {
  11.     protected bool $europeanCountriesOnly false;
  12.     /**
  13.      * @return array
  14.      */
  15.     protected function getAllowedCountries(): array
  16.     {
  17.         $europe = [
  18.             'DE''AT''CH',
  19.             'ES''LU''CZ',
  20.             'NL''IT''PL',
  21.             'BE''FR''FI',
  22.             'GB''DK''NO',
  23.             'HU''RO''LI',
  24.             'RU''SE''BG',
  25.             'IS',
  26.         ];
  27.         $allowed array_merge($europe, [
  28.             'TH''US''SK',
  29.             'PT''EG''SI',
  30.             'CA''AU',  'UA',
  31.             'PY''GR''EC',
  32.             'LV''LT''JP',
  33.             'MT''TR''AE',
  34.             'IE',  'CN',
  35.         ]);
  36.         $return = [];
  37.         if($this->getEuropeanCountriesOnly()) {
  38.             $allowed $europe;
  39.         }
  40.         foreach(Countries::getNames() as $code => $name) {
  41.             if(in_array($code$allowed)) {
  42.                 $return[$name] = $code;
  43.             }
  44.         }
  45.         return $return;
  46.     }
  47.     /**
  48.      * @return bool
  49.      */
  50.     protected function getEuropeanCountriesOnly(): bool
  51.     {
  52.         return $this->europeanCountriesOnly;
  53.     }
  54.     /**
  55.      * @return array
  56.      */
  57.     protected function getPreferredCountryChoices(): array
  58.     {
  59.         return ['DE''AT''CH'];
  60.     }
  61.     /**
  62.      * @param FormBuilderInterface $builder
  63.      * @param array $options
  64.      */
  65.     public function buildForm(FormBuilderInterface $builder, array $options)
  66.     {
  67.         $notBlank = new NotBlank();
  68.         $builder->add('street'TextType::class, array(
  69.             'label' => 'Straße',
  70.             'constraints' => $notBlank
  71.         ));
  72.         $builder->add('house_number'TextType::class, array(
  73.             'label' => 'Hausnummer',
  74.             'attr' => [
  75.                 'maxlength' => 6
  76.             ],
  77.             'constraints' => $notBlank
  78.         ));
  79.         $builder->add('zip_code'TextType::class, array(
  80.             'label' => 'Postleitzahl',
  81.             'constraints' => $notBlank
  82.         ));
  83.         $builder->add('city'TextType::class, array(
  84.             'label' => 'Ort',
  85.             'constraints' => $notBlank
  86.         ));
  87.         $builder->add('country'CountryType::class, [
  88.             'label' => 'Land',
  89.             'choices' => $this->getAllowedCountries(),
  90.             'preferred_choices' => $this->getPreferredCountryChoices()
  91.         ]);
  92.     }
  93. }