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