app/Customize/Form/Type/CustomAddressType.php line 26

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Customize\Form\Type;
  13. use Eccube\Common\EccubeConfig;
  14. use Eccube\Form\Type\Master\PrefType;
  15. use Symfony\Component\Form\AbstractType;
  16. use Symfony\Component\Form\Extension\Core\Type\TextType;
  17. use Symfony\Component\Form\FormBuilderInterface;
  18. use Symfony\Component\Form\FormInterface;
  19. use Symfony\Component\Form\FormView;
  20. use Symfony\Component\OptionsResolver\OptionsResolver;
  21. use Symfony\Component\Validator\Constraints as Assert;
  22. class CustomAddressType extends AbstractType
  23. {
  24.     /**
  25.      * @var array
  26.      */
  27.     protected $config;
  28.     /**
  29.      * {@inheritdoc}
  30.      *
  31.      * CustomAddressType constructor.
  32.      *
  33.      * @param EccubeConfig $eccubeConfig
  34.      */
  35.     public function __construct(EccubeConfig $eccubeConfig)
  36.     {
  37.         $this->config $eccubeConfig;
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     public function buildForm(FormBuilderInterface $builder, array $options)
  43.     {
  44.         // Check if address_type is overseas to determine if fields should be required
  45.         $isOverseas = (int)$options['address_type'] === 1;
  46.         
  47.         $options['pref_options']['required'] = $options['required'] && !$isOverseas;
  48.         $options['addr01_options']['required'] = $options['required'];
  49.         $options['addr02_options']['required'] = $options['required'];
  50.         // Add NotBlank constraint only if required and not overseas
  51.         if ($options['required'] && !$isOverseas) {
  52.             $options['pref_options']['constraints'] = array_merge([
  53.                 new Assert\NotBlank([]),
  54.             ], $options['pref_options']['constraints']);
  55.         }
  56.         if ($options['required']) {
  57.             $options['addr01_options']['constraints'] = array_merge([
  58.                 new Assert\NotBlank([]),
  59.             ], $options['addr01_options']['constraints']);
  60.             $options['addr02_options']['constraints'] = array_merge([
  61.                 new Assert\NotBlank([]),
  62.             ], $options['addr02_options']['constraints']);
  63.         }
  64.         if (!isset($options['options']['error_bubbling'])) {
  65.             $options['options']['error_bubbling'] = $options['error_bubbling'];
  66.         }
  67.         $builder
  68.             ->add($options['pref_name'], PrefType::class, array_merge_recursive($options['options'], $options['pref_options']))
  69.             ->add($options['addr01_name'], TextType::class, array_merge_recursive($options['options'], $options['addr01_options']))
  70.             ->add($options['addr02_name'], TextType::class, array_merge_recursive($options['options'], $options['addr02_options']))
  71.         ;
  72.         $builder->setAttribute('pref_name'$options['pref_name']);
  73.         $builder->setAttribute('addr01_name'$options['addr01_name']);
  74.         $builder->setAttribute('addr02_name'$options['addr02_name']);
  75.     }
  76.     /**
  77.      * {@inheritdoc}
  78.      */
  79.     public function buildView(FormView $viewFormInterface $form, array $options)
  80.     {
  81.         $builder $form->getConfig();
  82.         $view->vars['pref_name'] = $builder->getAttribute('pref_name');
  83.         $view->vars['addr01_name'] = $builder->getAttribute('addr01_name');
  84.         $view->vars['addr02_name'] = $builder->getAttribute('addr02_name');
  85.     }
  86.     /**
  87.      * {@inheritdoc}
  88.      */
  89.     public function configureOptions(OptionsResolver $resolver)
  90.     {
  91.         $resolver->setDefaults([
  92.             'options' => [],
  93.             'pref_options' => ['constraints' => [], 'attr' => ['class' => 'p-region-id']],
  94.             'addr01_options' => [
  95.                 'constraints' => [
  96.                     new Assert\Length(['max' => $this->config['eccube_address1_len']]),
  97.                 ],
  98.                 'attr' => [
  99.                     'class' => 'p-locality p-street-address',
  100.                     'placeholder' => 'common.address_sample_01',
  101.                 ],
  102.             ],
  103.             'addr02_options' => [
  104.                 'constraints' => [
  105.                     new Assert\Length(['max' => $this->config['eccube_address2_len']]),
  106.                 ],
  107.                 'attr' => [
  108.                     'class' => 'p-extended-address',
  109.                     'placeholder' => 'common.address_sample_02',
  110.                 ],
  111.             ],
  112.             'pref_name' => 'pref',
  113.             'addr01_name' => 'addr01',
  114.             'addr02_name' => 'addr02',
  115.             'error_bubbling' => false,
  116.             'inherit_data' => true,
  117.             'trim' => true,
  118.             'address_type' => 0// Default value
  119.         ]);
  120.     }
  121.     public function getBlockPrefix()
  122.     {
  123.         return 'address';
  124.     }
  125. }