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