app/Customize/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 Customize\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'] = false;
  44.         // required の場合は NotBlank も追加する
  45.         if ($options['required']) {
  46.             $options['lastname_options']['constraints'] = array_merge([
  47.                 new Assert\NotBlank(),
  48.             ], $options['firstname_options']['constraints']);
  49.             $options['firstname_options']['constraints'] = [];
  50.         }
  51.         if (!isset($options['options']['error_bubbling'])) {
  52.             $options['options']['error_bubbling'] = $options['error_bubbling'];
  53.         }
  54.         if (empty($options['lastname_name'])) {
  55.             $options['lastname_name'] = $builder->getName().'01';
  56.         }
  57.         if (empty($options['firstname_name'])) {
  58.             $options['firstname_name'] = $builder->getName().'02';
  59.         }
  60.         $builder
  61.             ->add($options['lastname_name'], TextType::class, array_merge_recursive($options['options'], $options['lastname_options']))
  62.             ->add($options['firstname_name'], TextType::class, array_merge_recursive($options['options'], $options['firstname_options']))
  63.         ;
  64.         $builder->setAttribute('lastname_name'$options['lastname_name']);
  65.         $builder->setAttribute('firstname_name'$options['firstname_name']);
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     public function buildView(FormView $viewFormInterface $form, array $options)
  71.     {
  72.         $builder $form->getConfig();
  73.         $view->vars['lastname_name'] = $builder->getAttribute('lastname_name');
  74.         $view->vars['firstname_name'] = $builder->getAttribute('firstname_name');
  75.     }
  76.     /**
  77.      * {@inheritdoc}
  78.      */
  79.     public function configureOptions(OptionsResolver $resolver)
  80.     {
  81.         $resolver->setDefaults([
  82.             'options' => [],
  83.             'lastname_options' => [
  84.                 'attr' => [
  85.                     'placeholder' => 'common.last_name',
  86.                 ],
  87.                 'constraints' => [
  88.                     new Assert\Length([
  89.                         'max' => $this->eccubeConfig['eccube_name_len'],
  90.                     ]),
  91.                     new Assert\Regex([
  92.                         'pattern' => '/^[^\s ]+$/u',
  93.                         'message' => 'form_error.not_contain_spaces',
  94.                     ]),
  95.                 ],
  96.             ],
  97.             'firstname_options' => [
  98.                 'attr' => [
  99.                     'placeholder' => 'common.first_name',
  100.                 ],
  101.                 'constraints' => [
  102.                     new Assert\Length([
  103.                         'max' => $this->eccubeConfig['eccube_name_len'],
  104.                     ]),
  105.                     new Assert\Regex([
  106.                         'pattern' => '/^[^\s ]+$/u',
  107.                         'message' => 'form_error.not_contain_spaces',
  108.                     ]),
  109.                 ],
  110.             ],
  111.             'lastname_name' => '',
  112.             'firstname_name' => '',
  113.             'error_bubbling' => false,
  114.             'inherit_data' => true,
  115.             'trim' => true,
  116.         ]);
  117.     }
  118.     /**
  119.      * {@inheritdoc}
  120.      */
  121.     public function getBlockPrefix()
  122.     {
  123.         return 'name';
  124.     }
  125. }