src/Eccube/Form/Type/PhoneNumberType.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 PhoneNumberType
  22.  */
  23. class PhoneNumberType extends AbstractType
  24. {
  25.     /**
  26.      * @var EccubeConfig
  27.      */
  28.     protected $eccubeConfig;
  29.     /**
  30.      * PhoneNumberType 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.         // 全角英数を事前に半角にする
  44.         $builder->addEventSubscriber(new \Eccube\Form\EventListener\ConvertKanaListener());
  45.         $builder->addEventSubscriber(new \Eccube\Form\EventListener\TruncateHyphenListener());
  46.     }
  47.     /**
  48.      * {@inheritdoc}
  49.      */
  50.     public function configureOptions(OptionsResolver $resolver)
  51.     {
  52.         $eccubeConfig $this->eccubeConfig;
  53.         $constraints = function (Options $options) use ($eccubeConfig) {
  54.             $constraints = [];
  55.             // requiredがtrueに指定されている場合, NotBlankを追加
  56.             if (isset($options['required']) && true === $options['required']) {
  57.                 $constraints[] = new Assert\NotBlank();
  58.             }
  59.             $constraints[] = new Assert\Length([
  60.                 'max' => $eccubeConfig['eccube_tel_len_max'],
  61.             ]);
  62.             $constraints[] = new Assert\Type([
  63.                 'type' => 'numeric',
  64.                 'message' => 'form_error.numeric_only',
  65.             ]);
  66.             return $constraints;
  67.         };
  68.         $resolver->setDefaults([
  69.             'options' => ['constraints' => []],
  70.             'constraints' => $constraints,
  71.             'attr' => [
  72.                 'placeholder' => 'common.phone_number_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 'phone_number';
  90.     }
  91. }