app/Customize/Form/Type/NameType.php line 13

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