src/Eccube/Form/Type/NameType.php line 25

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\Form\FormInterface;
  18. use Symfony\Component\Form\FormView;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. use Symfony\Component\Validator\Constraints as Assert;
  21. class NameType extends AbstractType
  22. {
  23.     /**
  24.      * @var EccubeConfig
  25.      */
  26.     protected $eccubeConfig;
  27.     /**
  28.      * NameType constructor.
  29.      *
  30.      * @param EccubeConfig $eccubeConfig
  31.      */
  32.     public function __construct(
  33.         EccubeConfig $eccubeConfig
  34.     ) {
  35.         $this->eccubeConfig $eccubeConfig;
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public function buildForm(FormBuilderInterface $builder, array $options)
  41.     {
  42.         $options['lastname_options']['required'] = $options['required'];
  43.         $options['firstname_options']['required'] = $options['required'];
  44.         // required の場合は NotBlank も追加する
  45.         if ($options['required']) {
  46.             $options['lastname_options']['constraints'] = array_merge([
  47.                 new Assert\NotBlank(),
  48.             ], $options['lastname_options']['constraints']);
  49.             $options['firstname_options']['constraints'] = array_merge([
  50.                 new Assert\NotBlank(),
  51.             ], $options['firstname_options']['constraints']);
  52.         }
  53.         if (!isset($options['options']['error_bubbling'])) {
  54.             $options['options']['error_bubbling'] = $options['error_bubbling'];
  55.         }
  56.         if (empty($options['lastname_name'])) {
  57.             $options['lastname_name'] = $builder->getName().'01';
  58.         }
  59.         if (empty($options['firstname_name'])) {
  60.             $options['firstname_name'] = $builder->getName().'02';
  61.         }
  62.         $builder
  63.             ->add($options['lastname_name'], TextType::class, array_merge_recursive($options['options'], $options['lastname_options']))
  64.             ->add($options['firstname_name'], TextType::class, array_merge_recursive($options['options'], $options['firstname_options']))
  65.         ;
  66.         $builder->setAttribute('lastname_name'$options['lastname_name']);
  67.         $builder->setAttribute('firstname_name'$options['firstname_name']);
  68.     }
  69.     /**
  70.      * {@inheritdoc}
  71.      */
  72.     public function buildView(FormView $viewFormInterface $form, array $options)
  73.     {
  74.         $builder $form->getConfig();
  75.         $view->vars['lastname_name'] = $builder->getAttribute('lastname_name');
  76.         $view->vars['firstname_name'] = $builder->getAttribute('firstname_name');
  77.     }
  78.     /**
  79.      * {@inheritdoc}
  80.      */
  81.     public function configureOptions(OptionsResolver $resolver)
  82.     {
  83.         $resolver->setDefaults([
  84.             'options' => [],
  85.             'lastname_options' => [
  86.                 'attr' => [
  87.                     'placeholder' => 'common.last_name',
  88.                 ],
  89.                 'constraints' => [
  90.                     new Assert\Length([
  91.                         'max' => $this->eccubeConfig['eccube_name_len'],
  92.                     ]),
  93.                     new Assert\Regex([
  94. /*▼RZ 2024.11.27 EDIT 名前にスペースを入力されるのを許可する▼*/
  95. /*
  96.                         'pattern' => '/^[^\s ]+$/u',
  97. */
  98.                         'pattern' => '/^[^ ]+$/u',
  99. /*▲RZ 2024.11.27 EDIT 名前にスペースを入力されるのを許可する▲*/
  100.                         'message' => 'form_error.not_contain_spaces',
  101.                     ]),
  102.                 ],
  103.             ],
  104.             'firstname_options' => [
  105.                 'attr' => [
  106.                     'placeholder' => 'common.first_name',
  107.                 ],
  108.                 'constraints' => [
  109.                     new Assert\Length([
  110.                         'max' => $this->eccubeConfig['eccube_name_len'],
  111.                     ]),
  112.                     new Assert\Regex([
  113. /*▼RZ 2024.11.27 EDIT 名前にスペースを入力されるのを許可する▼*/
  114. /*
  115.                         'pattern' => '/^[^\s ]+$/u',
  116. */
  117.                         'pattern' => '/^[^ ]+$/u',
  118. /*▲RZ 2024.11.27 EDIT 名前にスペースを入力されるのを許可する▲*/
  119.                         'message' => 'form_error.not_contain_spaces',
  120.                     ]),
  121.                 ],
  122.             ],
  123.             'lastname_name' => '',
  124.             'firstname_name' => '',
  125.             'error_bubbling' => false,
  126.             'inherit_data' => true,
  127.             'trim' => true,
  128.         ]);
  129.     }
  130.     /**
  131.      * {@inheritdoc}
  132.      */
  133.     public function getBlockPrefix()
  134.     {
  135.         return 'name';
  136.     }
  137. }