src/Eccube/Form/Type/Front/EntryType.php line 39

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\Front;
  13. use Eccube\Common\EccubeConfig;
  14. use Eccube\Entity\Customer;
  15. use Eccube\Form\Type\AddressType;
  16. use Eccube\Form\Type\KanaType;
  17. use Eccube\Form\Type\Master\JobType;
  18. use Eccube\Form\Type\Master\SexType;
  19. use Eccube\Form\Type\NameType;
  20. use Eccube\Form\Type\PhoneNumberType;
  21. use Eccube\Form\Type\PostalType;
  22. use Eccube\Form\Type\RepeatedEmailType;
  23. use Eccube\Form\Type\RepeatedPasswordType;
  24. use Symfony\Component\Form\AbstractType;
  25. use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
  26. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  27. use Symfony\Component\Form\Extension\Core\Type\TextType;
  28. use Symfony\Component\Form\FormBuilderInterface;
  29. use Symfony\Component\Form\FormError;
  30. use Symfony\Component\Form\FormEvent;
  31. use Symfony\Component\Form\FormEvents;
  32. use Symfony\Component\OptionsResolver\OptionsResolver;
  33. use Symfony\Component\Validator\Constraints as Assert;
  34. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  35. class EntryType extends AbstractType
  36. {
  37. /**
  38. * @var EccubeConfig
  39. */
  40. protected $eccubeConfig;
  41. /**
  42. * EntryType constructor.
  43. *
  44. * @param EccubeConfig $eccubeConfig
  45. */
  46. public function __construct(EccubeConfig $eccubeConfig)
  47. {
  48. $this->eccubeConfig = $eccubeConfig;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function buildForm(FormBuilderInterface $builder, array $options)
  54. {
  55. $builder
  56. ->add('name', NameType::class, [
  57. 'required' => true,
  58. ])
  59. ->add('kana', KanaType::class, [])
  60. ->add('is_company', ChoiceType::class, [
  61. 'choices' => [
  62. trans('admin.common.is_company_true') => true,
  63. trans('admin.common.is_company_false') => false,
  64. ],
  65. 'required' => false,
  66. 'placeholder' => false,
  67. 'expanded' => true,
  68. 'multiple' => false,
  69. 'label' => trans('admin.common.is_company'),
  70. ])
  71. ->add('company_name', TextType::class, [
  72. 'required' => false,
  73. 'constraints' => [
  74. new Assert\Length([
  75. 'max' => $this->eccubeConfig['eccube_stext_len'],
  76. ]),
  77. ],
  78. ])
  79. ->add('postal_code', PostalType::class)
  80. ->add('address', AddressType::class)
  81. ->add('phone_number', PhoneNumberType::class, [
  82. 'required' => true,
  83. ])
  84. ->add('email', RepeatedEmailType::class)
  85. ->add('plain_password', RepeatedPasswordType::class)
  86. ->add('birth', BirthdayType::class, [
  87. 'required' => false,
  88. 'input' => 'datetime',
  89. 'years' => range(date('Y'), date('Y') - $this->eccubeConfig['eccube_birth_max']),
  90. 'widget' => 'choice',
  91. 'placeholder' => ['year' => '----', 'month' => '--', 'day' => '--'],
  92. 'constraints' => [
  93. new Assert\LessThanOrEqual([
  94. 'value' => date('Y-m-d', strtotime('-1 day')),
  95. 'message' => 'form_error.select_is_future_or_now_date',
  96. ]),
  97. ],
  98. ])
  99. ->add('sex', SexType::class, [
  100. 'required' => false,
  101. ])
  102. ->add('job', JobType::class, [
  103. 'required' => false,
  104. ]);
  105. $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
  106. $Customer = $event->getData();
  107. if ($Customer instanceof Customer && !$Customer->getId()) {
  108. $form = $event->getForm();
  109. $form->add('user_policy_check', CheckboxType::class, [
  110. 'required' => true,
  111. 'label' => false,
  112. 'mapped' => false,
  113. 'constraints' => [
  114. new Assert\NotBlank(),
  115. ],
  116. ]);
  117. $form->add('mailmaga_flg', CheckboxType::class, [
  118. 'required' => false,
  119. 'label' => '受け取り可',
  120. ]);
  121. }
  122. }
  123. );
  124. $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  125. $form = $event->getForm();
  126. /** @var Customer $Customer */
  127. $Customer = $event->getData();
  128. if ($Customer->getPlainPassword() != '' && $Customer->getPlainPassword() == $Customer->getEmail()) {
  129. $form['plain_password']['first']->addError(new FormError(trans('common.password_eq_email')));
  130. }
  131. });
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function configureOptions(OptionsResolver $resolver)
  137. {
  138. $resolver->setDefaults([
  139. 'data_class' => 'Eccube\Entity\Customer',
  140. ]);
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function getBlockPrefix()
  146. {
  147. // todo entry,mypageで共有されているので名前を変更する
  148. return 'entry';
  149. }
  150. }