app/Customize/Form/Type/AddressType.php line 14

Open in your IDE?
  1. <?php
  2. namespace Customize\Form\Type;
  3. use Eccube\Common\EccubeConfig;
  4. use Eccube\Form\Type\Master\PrefType;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\Extension\Core\Type\TextType;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Symfony\Component\Form\FormInterface;
  9. use Symfony\Component\Form\FormView;
  10. use Symfony\Component\OptionsResolver\OptionsResolver;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. class AddressType extends AbstractType
  13. {
  14.     /**
  15.      * @var array
  16.      */
  17.     protected $config;
  18.     /**
  19.      * {@inheritdoc}
  20.      *
  21.      * AddressType constructor.
  22.      *
  23.      * @param EccubeConfig $eccubeConfig
  24.      */
  25.     public function __construct(EccubeConfig $eccubeConfig)
  26.     {
  27.         $this->config $eccubeConfig;
  28.     }
  29.     /**
  30.      * {@inheritdoc}
  31.      */
  32.     public function buildForm(FormBuilderInterface $builder, array $options)
  33.     {
  34.         $options['pref_options']['required'] = $options['required'];
  35.         $options['addr01_options']['required'] = $options['required'];
  36.         $options['addr02_options']['required'] = $options['required'];
  37.         // required の場合は NotBlank も追加する
  38.         if ($options['required']) {
  39.             $options['pref_options']['constraints'] = array_merge([
  40.                 new Assert\NotBlank([]),
  41.             ], $options['pref_options']['constraints']);
  42.             $options['addr01_options']['constraints'] = array_merge([
  43.                 new Assert\NotBlank([]),
  44.             ], $options['addr01_options']['constraints']);
  45.             $options['addr02_options']['constraints'] = array_merge([
  46.                 new Assert\NotBlank([]),
  47.             ], $options['addr02_options']['constraints']);
  48.         }
  49.         if (!isset($options['options']['error_bubbling'])) {
  50.             $options['options']['error_bubbling'] = $options['error_bubbling'];
  51.         }
  52.         $builder
  53.             ->add($options['pref_name'], PrefType::class, array_merge_recursive($options['options'], $options['pref_options']))
  54.             ->add($options['addr01_name'], TextType::class, array_merge_recursive($options['options'], $options['addr01_options']))
  55.             ->add($options['addr02_name'], TextType::class, array_merge_recursive($options['options'], $options['addr02_options']))
  56.         ;
  57.         $builder->setAttribute('pref_name'$options['pref_name']);
  58.         $builder->setAttribute('addr01_name'$options['addr01_name']);
  59.         $builder->setAttribute('addr02_name'$options['addr02_name']);
  60.     }
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     public function buildView(FormView $viewFormInterface $form, array $options)
  65.     {
  66.         $builder $form->getConfig();
  67.         $view->vars['pref_name'] = $builder->getAttribute('pref_name');
  68.         $view->vars['addr01_name'] = $builder->getAttribute('addr01_name');
  69.         $view->vars['addr02_name'] = $builder->getAttribute('addr02_name');
  70.     }
  71.     /**
  72.      * {@inheritdoc}
  73.      */
  74.     public function configureOptions(OptionsResolver $resolver)
  75.     {
  76.         $resolver->setDefaults([
  77.             'options' => [],
  78.             'pref_options' => [
  79.                 'constraints' => [new Assert\NotBlank(),], 'attr' => ['class' => 'p-region-id']
  80.             ],
  81.             'addr01_options' => [
  82.                 'constraints' => [
  83.                     new Assert\NotBlank(),
  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.             'addr02_options' => [
  92.                 'constraints' => [
  93.                     new Assert\NotBlank(),
  94.                     new Assert\Length(['max' => $this->config['eccube_address2_len']]),
  95.                 ],
  96.                 'attr' => [
  97.                     'class' => 'p-extended-address',
  98.                     'placeholder' => 'common.address_sample_02',
  99.                 ],
  100.             ],
  101.             'pref_name' => 'pref',
  102.             'addr01_name' => 'addr01',
  103.             'addr02_name' => 'addr02',
  104.             'error_bubbling' => false,
  105.             'inherit_data' => true,
  106.             'trim' => true,
  107.         ]);
  108.     }
  109.     public function getBlockPrefix()
  110.     {
  111.         return 'address';
  112.     }
  113. }