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