src/Eccube/Form/Type/AddressType.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 Eccube\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 AddressType extends AbstractType
  23. {
  24.     /**
  25.      * @var array
  26.      */
  27.     protected $config;
  28.     /**
  29.      * {@inheritdoc}
  30.      *
  31.      * AddressType 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.         $options['pref_options']['required'] = $options['required'];
  45.         $options['addr01_options']['required'] = $options['required'];
  46.         $options['addr02_options']['required'] = false;
  47.         // required の場合は NotBlank も追加する
  48.         if ($options['required']) {
  49.             $options['pref_options']['constraints'] = array_merge([
  50.                 new Assert\NotBlank([]),
  51.             ], $options['pref_options']['constraints']);
  52.             $options['addr01_options']['constraints'] = array_merge([
  53.                 new Assert\NotBlank([]),
  54.             ], $options['addr01_options']['constraints']);
  55.             $options['addr02_options']['constraints'] = [];
  56.         }
  57.         if (!isset($options['options']['error_bubbling'])) {
  58.             $options['options']['error_bubbling'] = $options['error_bubbling'];
  59.         }
  60.         $builder
  61.             ->add($options['pref_name'], PrefType::class, array_merge_recursive($options['options'], $options['pref_options']))
  62.             ->add($options['addr01_name'], TextType::class, array_merge_recursive($options['options'], $options['addr01_options']))
  63.             ->add($options['addr02_name'], TextType::class, array_merge_recursive($options['options'], $options['addr02_options']))
  64.         ;
  65.         $builder->setAttribute('pref_name'$options['pref_name']);
  66.         $builder->setAttribute('addr01_name'$options['addr01_name']);
  67.         $builder->setAttribute('addr02_name'$options['addr02_name']);
  68.     }
  69.     /**
  70.      * {@inheritdoc}
  71.      */
  72.     public function buildView(FormView $viewFormInterface $form, array $options)
  73.     {
  74.         $builder $form->getConfig();
  75.         $view->vars['pref_name'] = $builder->getAttribute('pref_name');
  76.         $view->vars['addr01_name'] = $builder->getAttribute('addr01_name');
  77.         $view->vars['addr02_name'] = $builder->getAttribute('addr02_name');
  78.     }
  79.     /**
  80.      * {@inheritdoc}
  81.      */
  82.     public function configureOptions(OptionsResolver $resolver)
  83.     {
  84.         $resolver->setDefaults([
  85.             'options' => [],
  86.             'pref_options' => ['constraints' => [], 'attr' => ['class' => 'p-region-id']],
  87.             'addr01_options' => [
  88.                 'constraints' => [
  89.                     new Assert\Length(['max' => $this->config['eccube_address1_len']]),
  90.                 ],
  91.                 'attr' => [
  92.                     'class' => 'p-locality p-street-address',
  93.                     'placeholder' => 'common.address_sample_01',
  94.                 ],
  95.             ],
  96.             'addr02_options' => [
  97.                 'constraints' => [
  98.                     new Assert\Length(['max' => $this->config['eccube_address2_len']]),
  99.                 ],
  100.                 'attr' => [
  101.                     'class' => 'p-extended-address',
  102.                     'placeholder' => 'common.address_sample_02',
  103.                 ],
  104.             ],
  105.             'pref_name' => 'pref',
  106.             'addr01_name' => 'addr01',
  107.             'addr02_name' => 'addr02',
  108.             'error_bubbling' => false,
  109.             'inherit_data' => true,
  110.             'trim' => true,
  111.         ]);
  112.     }
  113.     public function getBlockPrefix()
  114.     {
  115.         return 'address';
  116.     }
  117. }