src/Eccube/Form/Type/PostalType.php line 27

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 Symfony\Component\Form\AbstractType;
  15. use Symfony\Component\Form\Extension\Core\Type\TextType;
  16. use Symfony\Component\Form\FormBuilderInterface;
  17. use Symfony\Component\OptionsResolver\Options;
  18. use Symfony\Component\OptionsResolver\OptionsResolver;
  19. use Symfony\Component\Validator\Constraints as Assert;
  20. /**
  21.  * Class ZipType
  22.  */
  23. class PostalType extends AbstractType
  24. {
  25.     /**
  26.      * @var EccubeConfig
  27.      */
  28.     protected $eccubeConfig;
  29.     /**
  30.      * ZipType constructor.
  31.      *
  32.      * @param EccubeConfig $eccubeConfig
  33.      */
  34.     public function __construct(EccubeConfig $eccubeConfig)
  35.     {
  36.         $this->eccubeConfig $eccubeConfig;
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function buildForm(FormBuilderInterface $builder, array $options)
  42.     {
  43.         $builder->addEventSubscriber(new \Eccube\Form\EventListener\ConvertKanaListener());
  44.         $builder->addEventSubscriber(new \Eccube\Form\EventListener\TruncateHyphenListener());
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     public function configureOptions(OptionsResolver $resolver)
  50.     {
  51.         $eccubeConfig $this->eccubeConfig;
  52.         $constraints = function (Options $options) use ($eccubeConfig) {
  53.             $constraints = [];
  54.             // requiredがtrueに指定されている場合, NotBlankを追加
  55.             if (isset($options['required']) && true === $options['required']) {
  56.                 $constraints[] = new Assert\NotBlank();
  57.             }
  58.             $constraints[] = new Assert\Length([
  59.                 'max' => $eccubeConfig['eccube_postal_code'],
  60.             ]);
  61.             $constraints[] = new Assert\Type([
  62.                 'type' => 'numeric',
  63.                 'message' => 'form_error.numeric_only',
  64.             ]);
  65.             return $constraints;
  66.         };
  67.         $resolver->setDefaults([
  68.             'options' => [],
  69.             'constraints' => $constraints,
  70.             'attr' => [
  71.                 'class' => 'p-postal-code',
  72.                 'placeholder' => 'common.postal_code_sample',
  73.             ],
  74.             'trim' => true,
  75.         ]);
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     public function getParent()
  81.     {
  82.         return TextType::class;
  83.     }
  84.     /**
  85.      * {@inheritdoc}
  86.      */
  87.     public function getBlockPrefix()
  88.     {
  89.         return 'postal';
  90.     }
  91. }