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.                         'pattern' => '/^[^\s ]+$/u',
  95.                         'message' => 'form_error.not_contain_spaces',
  96.                     ]),
  97.                 ],
  98.             ],
  99.             'firstname_options' => [
  100.                 'attr' => [
  101.                     'placeholder' => 'common.first_name',
  102.                 ],
  103.                 'constraints' => [
  104.                     new Assert\Length([
  105.                         'max' => $this->eccubeConfig['eccube_name_len'],
  106.                     ]),
  107.                     new Assert\Regex([
  108.                         'pattern' => '/^[^\s ]+$/u',
  109.                         'message' => 'form_error.not_contain_spaces',
  110.                     ]),
  111.                 ],
  112.             ],
  113.             'lastname_name' => '',
  114.             'firstname_name' => '',
  115.             'error_bubbling' => false,
  116.             'inherit_data' => true,
  117.             'trim' => true,
  118.         ]);
  119.     }
  120.     /**
  121.      * {@inheritdoc}
  122.      */
  123.     public function getBlockPrefix()
  124.     {
  125.         return 'name';
  126.     }
  127. }