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