src/Eccube/Form/Type/PhoneNumberType.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 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 PhoneNumberType
  21. */
  22. class PhoneNumberType extends AbstractType
  23. {
  24. /**
  25. * @var EccubeConfig
  26. */
  27. protected $eccubeConfig;
  28. /**
  29. * PhoneNumberType 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. // 全角英数を事前に半角にする
  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. $resolver->setNormalizer('constraints', function($options, $value) {
  52. $constraints = [];
  53. // requiredがtrueに指定されている場合, NotBlankを追加
  54. if (isset($options['required']) && true === $options['required']) {
  55. $constraints[] = new Assert\NotBlank();
  56. }
  57. $constraints[] = new Assert\Length([
  58. 'max' => $this->eccubeConfig['eccube_tel_len_max'],
  59. ]);
  60. $constraints[] = new Assert\Type([
  61. 'type' => 'digit',
  62. 'message' => 'form_error.numeric_only',
  63. ]);
  64. return array_merge($constraints, $value);
  65. });
  66. $resolver->setDefaults([
  67. 'attr' => [
  68. 'placeholder' => 'common.phone_number_sample',
  69. ],
  70. 'trim' => true,
  71. ]);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getParent()
  77. {
  78. return TelType::class;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function getBlockPrefix()
  84. {
  85. return 'phone_number';
  86. }
  87. }