app/Customize/Form/ContactPhoneNumberType.php line 27

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;
  13. use Eccube\Common\EccubeConfig;
  14. use Eccube\Form\Type\PhoneNumberType;
  15. use Eccube\Form\Type\MasterType;
  16. use Symfony\Component\Form\AbstractType;
  17. use Symfony\Component\Form\Extension\Core\Type\TextType;
  18. use Symfony\Component\Form\FormBuilderInterface;
  19. use Symfony\Component\Form\FormInterface;
  20. use Symfony\Component\Form\FormView;
  21. use Symfony\Component\OptionsResolver\OptionsResolver;
  22. use Symfony\Component\Validator\Constraints as Assert;
  23. class ContactPhoneNumberType extends AbstractType
  24. {
  25.     /**
  26.      * @var EccubeConfig
  27.      */
  28.     protected $eccubeConfig;
  29.     /**
  30.      * NameType constructor.
  31.      *
  32.      * @param EccubeConfig $eccubeConfig
  33.      */
  34.     public function __construct(
  35.         EccubeConfig $eccubeConfig
  36.     ) {
  37.         $this->eccubeConfig $eccubeConfig;
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     public function buildForm(FormBuilderInterface $builder, array $options)
  43.     {
  44.         $options['first_num_options']['required'] = $options['required'];
  45.         $options['second_num_options']['required'] = $options['required'];
  46.         $options['last_num_options']['required'] = $options['required'];
  47.         // required の場合は NotBlank も追加する
  48.         if ($options['required']) {
  49.             $options['first_num_options']['constraints'] = array_merge([
  50.                 new Assert\NotBlank(),
  51.             ], $options['first_num_options']['constraints']);
  52.             $options['second_num_options']['constraints'] = array_merge([
  53.                 new Assert\NotBlank(),
  54.             ], $options['second_num_options']['constraints']);
  55.             $options['last_num_options']['constraints'] = array_merge([
  56.                 new Assert\NotBlank(),
  57.             ], $options['last_num_options']['constraints']);
  58.         }
  59.         if (!isset($options['options']['error_bubbling'])) {
  60.             $options['options']['error_bubbling'] = $options['error_bubbling'];
  61.         }
  62.         if (empty($options['first_num'])) {
  63.             $options['first_num'] = $builder->getName().'01';
  64.         }
  65.         if (empty($options['second_num'])) {
  66.             $options['second_num'] = $builder->getName().'02';
  67.         }
  68.         if (empty($options['last_num'])) {
  69.             $options['last_num'] = $builder->getName().'03';
  70.         }
  71.         $builder
  72.             ->add($options['first_num'], TextType::class, array_merge_recursive($options['options'], $options['first_num_options']))
  73.             ->add($options['second_num'], TextType::class, array_merge_recursive($options['options'], $options['second_num_options']))
  74.             ->add($options['last_num'], TextType::class, array_merge_recursive($options['options'], $options['last_num_options']))
  75.         ;
  76.         $builder->setAttribute('first_num'$options['first_num_options']);
  77.         $builder->setAttribute('second_num'$options['second_num_options']);
  78.         $builder->setAttribute('last_num'$options['last_num_options']);
  79.     }
  80.     /**
  81.      * {@inheritdoc}
  82.      */
  83.     public function buildView(FormView $viewFormInterface $form, array $options)
  84.     {
  85.         $builder $form->getConfig();
  86.         $view->vars['first_num'] = $builder->getAttribute('first_num');
  87.         $view->vars['second_num'] = $builder->getAttribute('second_num');
  88.         $view->vars['last_num'] = $builder->getAttribute('last_num');
  89.     }
  90.     /**
  91.      * {@inheritdoc}
  92.      */
  93.     public function configureOptions(OptionsResolver $resolver)
  94.     {
  95.         $resolver->setDefaults([
  96.             'options' => [
  97.                 'label' => '',
  98.             ],
  99.             'last_num_options' => [
  100.                 'attr' => [
  101.                     'label' => '',
  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.             'second_num_options' => [
  114.                 'attr' => [
  115.                     'label' => '',
  116.                 ],
  117.                 'constraints' => [
  118.                     new Assert\Length([
  119.                         'max' => $this->eccubeConfig['eccube_name_len'],
  120.                     ]),
  121.                     new Assert\Regex([
  122.                         'pattern' => '/^[^\s ]+$/u',
  123.                         'message' => 'form_error.not_contain_spaces',
  124.                     ]),
  125.                 ],
  126.             ],
  127.             'first_num_options' => [
  128.                 'attr' => [
  129.                     'label' => '',
  130.                 ],
  131.                 'constraints' => [
  132.                     new Assert\Length([
  133.                         'max' => $this->eccubeConfig['eccube_name_len'],
  134.                     ]),
  135.                     new Assert\Regex([
  136.                         'pattern' => '/^[^\s ]+$/u',
  137.                         'message' => 'form_error.not_contain_spaces',
  138.                     ]),
  139.                 ],
  140.             ],
  141.             'last_num' => '',
  142.             'second_num' => '',
  143.             'first_num' => '',
  144.             'error_bubbling' => false,
  145.             'inherit_data' => true,
  146.             'trim' => true,
  147.             'compound' => true,
  148.         ]);
  149.     }
  150.     public function getBlockPrefix()
  151.     {
  152.         return 'contact_phone_number';
  153.     }
  154. }