app/Customize/Form/Type/CustomPostalType.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 Customize\Form\Type;
  13. use Eccube\Common\EccubeConfig;
  14. use Symfony\Component\Form\AbstractType;
  15. use Symfony\Component\Form\Extension\Core\Type\TelType;
  16. use Symfony\Component\Form\FormBuilderInterface;
  17. use Symfony\Component\OptionsResolver\OptionsResolver;
  18. use Symfony\Component\Validator\Constraints as Assert;
  19. /**
  20.  * Class CustomPostalType
  21.  */
  22. class CustomPostalType extends AbstractType
  23. {
  24.     /**
  25.      * @var EccubeConfig
  26.      */
  27.     protected $eccubeConfig;
  28.     /**
  29.      * CustomPostalType constructor.
  30.      *
  31.      * @param EccubeConfig $eccubeConfig
  32.      */
  33.     public function __construct(EccubeConfig $eccubeConfig)
  34.     {
  35.         $this->eccubeConfig $eccubeConfig;
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public function buildForm(FormBuilderInterface $builder, array $options)
  41.     {
  42.         $builder->addEventSubscriber(new \Eccube\Form\EventListener\ConvertKanaListener());
  43.         $builder->addEventSubscriber(new \Eccube\Form\EventListener\TruncateHyphenListener());
  44.     }
  45.     /**
  46.      * {@inheritdoc}
  47.      */
  48.     public function configureOptions(OptionsResolver $resolver)
  49.     {
  50.         $resolver->setNormalizer('constraints', function($options$value) {
  51.             $constraints = [];
  52.             
  53.             // Check if address_type is overseas to determine if NotBlank should be added
  54.             $isOverseas = isset($options['address_type']) && (int)$options['address_type'] === 1;
  55.             
  56.             // Add NotBlank only if required and not overseas
  57.             if (isset($options['required']) && true === $options['required'] && !$isOverseas) {
  58.                 $constraints[] = new Assert\NotBlank();
  59.             }
  60.             $constraints[] = new Assert\Length([
  61.                 'max' => $this->eccubeConfig['eccube_postal_code'],
  62.             ]);
  63.             // Only add numeric validation for domestic addresses
  64.             if (!$isOverseas) {
  65.                 $constraints[] = new Assert\Type([
  66.                     'type' => 'digit',
  67.                     'message' => 'form_error.numeric_only',
  68.                 ]);
  69.             }
  70.             return array_merge($constraints$value);
  71.         });
  72.         $resolver->setDefaults([
  73.             'options' => [],
  74.             'attr' => [
  75.                 'class' => 'p-postal-code',
  76.                 'placeholder' => 'common.postal_code_sample',
  77.             ],
  78.             'trim' => true,
  79.             'address_type' => 0// Default value
  80.         ]);
  81.     }
  82.     /**
  83.      * {@inheritdoc}
  84.      */
  85.     public function getParent()
  86.     {
  87.         return TelType::class;
  88.     }
  89.     /**
  90.      * {@inheritdoc}
  91.      */
  92.     public function getBlockPrefix()
  93.     {
  94.         return 'postal';
  95.     }
  96. }