app/Plugin/Paid4/Form/Type/PaidNameType.php line 19

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright(c) 2020 RACCOON HOLDINGS, Inc. All rights reserved.
  4.  * https://paid.jp/
  5.  */
  6. namespace Plugin\Paid4\Form\Type;
  7. use Eccube\Common\EccubeConfig;
  8. use Symfony\Component\Form\AbstractType;
  9. use Symfony\Component\Form\Extension\Core\Type\TextType;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\Form\FormView;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. class PaidNameType extends AbstractType
  16. {
  17.     /**
  18.      * @var EccubeConfig
  19.      */
  20.     protected $eccubeConfig;
  21.     /**
  22.      * NameType constructor.
  23.      *
  24.      * @param EccubeConfig $eccubeConfig
  25.      */
  26.     public function __construct(
  27.         EccubeConfig $eccubeConfig
  28.     )
  29.     {
  30.         $this->eccubeConfig $eccubeConfig;
  31.     }
  32.     /**
  33.      * @param FormBuilderInterface $builder
  34.      * @param array $options
  35.      */
  36.     public function buildForm(FormBuilderInterface $builder, array $options)
  37.     {
  38.         $options['lastname_options']['required'] = $options['required'];
  39.         $options['firstname_options']['required'] = $options['required'];
  40.         // required の場合は NotBlank も追加する
  41.         if ($options['required']) {
  42.             $options['lastname_options']['constraints'] = array_merge([
  43.                 new Assert\NotBlank(),
  44.             ], $options['lastname_options']['constraints']);
  45.             $options['firstname_options']['constraints'] = array_merge([
  46.                 new Assert\NotBlank(),
  47.             ], $options['firstname_options']['constraints']);
  48.         }
  49.         if (!isset($options['options']['error_bubbling'])) {
  50.             $options['options']['error_bubbling'] = $options['error_bubbling'];
  51.         }
  52.         if (empty($options['lastname_name'])) {
  53.             $options['lastname_name'] = $builder->getName() . '_sei';
  54.         }
  55.         if (empty($options['firstname_name'])) {
  56.             $options['firstname_name'] = $builder->getName() . '_mei';
  57.         }
  58.         $builder
  59.             ->add($options['lastname_name'], TextType::class, array_merge_recursive($options['options'], $options['lastname_options']))
  60.             ->add($options['firstname_name'], TextType::class, array_merge_recursive($options['options'], $options['firstname_options']));
  61.         $builder->setAttribute('lastname_name'$options['lastname_name']);
  62.         $builder->setAttribute('firstname_name'$options['firstname_name']);
  63.     }
  64.     /**
  65.      * @param FormView $view
  66.      * @param FormInterface $form
  67.      * @param array $options
  68.      */
  69.     public function buildView(FormView $viewFormInterface $form, array $options)
  70.     {
  71.         $builder $form->getConfig();
  72.         $view->vars['lastname_name'] = $builder->getAttribute('lastname_name');
  73.         $view->vars['firstname_name'] = $builder->getAttribute('firstname_name');
  74.     }
  75.     /**
  76.      * @param OptionsResolver $resolver
  77.      */
  78.     public function configureOptions(OptionsResolver $resolver)
  79.     {
  80.         $resolver->setDefaults([
  81.             'options' => [],
  82.             'lastname_options' => [
  83.                 'attr' => [
  84.                     'placeholder' => 'common.last_name',
  85.                 ],
  86.                 'constraints' => [
  87.                     new Assert\Length([
  88.                         'max' => $this->eccubeConfig['paid4.stext_len'],
  89.                     ]),
  90.                     new Assert\Regex([
  91.                         'pattern' => '/^[^\s ]+$/u',
  92.                         'message' => 'form_error.not_contain_spaces',
  93.                     ]),
  94.                 ],
  95.             ],
  96.             'firstname_options' => [
  97.                 'attr' => [
  98.                     'placeholder' => 'common.first_name',
  99.                 ],
  100.                 'constraints' => [
  101.                     new Assert\Length([
  102.                         'max' => $this->eccubeConfig['paid4.stext_len'],
  103.                     ]),
  104.                     new Assert\Regex([
  105.                         'pattern' => '/^[^\s ]+$/u',
  106.                         'message' => 'form_error.not_contain_spaces',
  107.                     ]),
  108.                 ],
  109.             ],
  110.             'lastname_name' => '',
  111.             'firstname_name' => '',
  112.             'error_bubbling' => false,
  113.             'inherit_data' => true,
  114.             'trim' => true,
  115.         ]);
  116.     }
  117.     /**
  118.      * {@inheritdoc}
  119.      */
  120.     public function getBlockPrefix()
  121.     {
  122.         return 'name';
  123.     }
  124. }