vendor/sylius/sylius/src/Sylius/Bundle/AddressingBundle/Form/Type/AddressType.php line 23

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\AddressingBundle\Form\Type;
  12. use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Form\Extension\Core\Type\TextType;
  15. use Symfony\Component\Form\FormBuilderInterface;
  16. use Symfony\Component\OptionsResolver\Options;
  17. use Symfony\Component\OptionsResolver\OptionsResolver;
  18. final class AddressType extends AbstractResourceType
  19. {
  20.     /**
  21.      * @param string[] $validationGroups
  22.      */
  23.     public function __construct(string $dataClass, array $validationGroups, private EventSubscriberInterface $buildAddressFormSubscriber)
  24.     {
  25.         parent::__construct($dataClass$validationGroups);
  26.     }
  27.     public function buildForm(FormBuilderInterface $builder, array $options): void
  28.     {
  29.         $builder
  30.             ->add('firstName'TextType::class, [
  31.                 'label' => 'sylius.form.address.first_name',
  32.             ])
  33.             ->add('lastName'TextType::class, [
  34.                 'label' => 'sylius.form.address.last_name',
  35.             ])
  36.             ->add('phoneNumber'TextType::class, [
  37.                 'required' => false,
  38.                 'label' => 'sylius.form.address.phone_number',
  39.             ])
  40.             ->add('company'TextType::class, [
  41.                 'required' => false,
  42.                 'label' => 'sylius.form.address.company',
  43.             ])
  44.             ->add('countryCode'CountryCodeChoiceType::class, [
  45.                 'label' => 'sylius.form.address.country',
  46.                 'enabled' => true,
  47.             ])
  48.             ->add('street'TextType::class, [
  49.                 'label' => 'sylius.form.address.street',
  50.             ])
  51.             ->add('city'TextType::class, [
  52.                 'label' => 'sylius.form.address.city',
  53.             ])
  54.             ->add('postcode'TextType::class, [
  55.                 'label' => 'sylius.form.address.postcode',
  56.             ])
  57.             ->addEventSubscriber($this->buildAddressFormSubscriber)
  58.         ;
  59.     }
  60.     public function configureOptions(OptionsResolver $resolver): void
  61.     {
  62.         parent::configureOptions($resolver);
  63.         $resolver
  64.             ->setDefaults([
  65.                 'validation_groups' => function (Options $options) {
  66.                     if ($options['shippable']) {
  67.                         return array_merge($this->validationGroups, ['shippable']);
  68.                     }
  69.                     return $this->validationGroups;
  70.                 },
  71.                 'shippable' => false,
  72.             ])
  73.             ->setAllowedTypes('shippable''bool')
  74.         ;
  75.     }
  76.     public function getBlockPrefix(): string
  77.     {
  78.         return 'sylius_address';
  79.     }
  80. }