Skip to content

Commit

Permalink
設定値に基づいてフォームのカナを必須項目かどうか決定
Browse files Browse the repository at this point in the history
  • Loading branch information
eternalharvest committed Aug 11, 2020
1 parent d677265 commit f38f876
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 58 deletions.
36 changes: 21 additions & 15 deletions src/Eccube/Controller/NonMemberShoppingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

namespace Eccube\Controller;

use Eccube\Entity\BaseInfo;
use Eccube\Entity\Customer;
use Eccube\Repository\BaseInfoRepository;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Form\Type\Front\NonMemberType;
Expand Down Expand Up @@ -49,21 +51,29 @@ class NonMemberShoppingController extends AbstractShoppingController
*/
protected $cartService;

/**
* @var BaseInfo
*/
protected $baseInfo;

/**
* NonMemberShoppingController constructor.
*
* @param ValidatorInterface $validator
* @param BaseInfoRepository $baseInfoRepository
* @param PrefRepository $prefRepository
* @param OrderHelper $orderHelper
* @param CartService $cartService
*/
public function __construct(
ValidatorInterface $validator,
BaseInfoRepository $baseInfoRepository,
PrefRepository $prefRepository,
OrderHelper $orderHelper,
CartService $cartService
) {
$this->validator = $validator;
$this->baseInfo = $baseInfoRepository->get();
$this->prefRepository = $prefRepository;
$this->orderHelper = $orderHelper;
$this->cartService = $cartService;
Expand Down Expand Up @@ -269,24 +279,20 @@ protected function customerValidation(array &$data)
]
);

$data['customer_kana01'] = mb_convert_kana($data['customer_kana01'], 'CV', 'utf-8');
$errors[] = $this->validator->validate(
$data['customer_kana01'],
[
new Assert\NotBlank(),
$kanaValidators = [
new Assert\Length(['max' => $this->eccubeConfig['eccube_kana_len']]),
new Assert\Regex(['pattern' => '/^[ァ-ヶヲ-゚ー]+$/u']),
]
);
$data['customer_kana02'] = mb_convert_kana($data['customer_kana02'], 'CV', 'utf-8');
$errors[] = $this->validator->validate(
$data['customer_kana02'],
[
new Assert\NotBlank(),
new Assert\Length(['max' => $this->eccubeConfig['eccube_kana_len']]),
new Assert\Regex(['pattern' => '/^[ァ-ヶヲ-゚ー]+$/u']),
]);
];

if ($this->baseInfo->isOptionRequireKana()) {
$kanaValidators []= new Assert\NotBlank();
}

$data['customer_kana01'] = mb_convert_kana($data['customer_kana01'], 'CV', 'utf-8');
$errors[] = $this->validator->validate($data['customer_kana01'], $kanaValidators);

$data['customer_kana02'] = mb_convert_kana($data['customer_kana02'], 'CV', 'utf-8');
$errors[] = $this->validator->validate($data['customer_kana02'], $kanaValidators);
$errors[] = $this->validator->validate(
$data['customer_company_name'],
[
Expand Down
4 changes: 1 addition & 3 deletions src/Eccube/Form/Type/Admin/CustomerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('name', NameType::class, [
'required' => true,
])
->add('kana', KanaType::class, [
'required' => true,
])
->add('kana', KanaType::class)
->add('company_name', TextType::class, [
'required' => false,
'constraints' => [
Expand Down
9 changes: 1 addition & 8 deletions src/Eccube/Form/Type/Admin/OrderType.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
],
],
])
->add('kana', KanaType::class, [
'required' => false,
'options' => [
'constraints' => [
new Assert\NotBlank(),
],
],
])
->add('kana', KanaType::class)
->add('company_name', TextType::class, [
'required' => false,
'constraints' => [
Expand Down
18 changes: 2 additions & 16 deletions src/Eccube/Form/Type/Admin/ShippingType.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,8 @@ public function __construct(
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', NameType::class, [
'required' => false,
'options' => [
'constraints' => [
new Assert\NotBlank(),
],
],
])
->add('kana', KanaType::class, [
'required' => false,
'options' => [
'constraints' => [
new Assert\NotBlank(),
],
],
])
->add('name', NameType::class)
->add('kana', KanaType::class)
->add('company_name', TextType::class, [
'required' => false,
'constraints' => [
Expand Down
4 changes: 1 addition & 3 deletions src/Eccube/Form/Type/Front/ContactType.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('name', NameType::class, [
'required' => true,
])
->add('kana', KanaType::class, [
'required' => false,
])
->add('kana', KanaType::class)
->add('postal_code', PostalType::class, [
'required' => false,
])
Expand Down
4 changes: 1 addition & 3 deletions src/Eccube/Form/Type/Front/CustomerAddressType.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('name', NameType::class, [
'required' => true,
])
->add('kana', KanaType::class, [
'required' => true,
])
->add('kana', KanaType::class)
->add('company_name', TextType::class, [
'required' => false,
'constraints' => [
Expand Down
2 changes: 1 addition & 1 deletion src/Eccube/Form/Type/Front/EntryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('name', NameType::class, [
'required' => true,
])
->add('kana', KanaType::class, [])
->add('kana', KanaType::class)
->add('company_name', TextType::class, [
'required' => false,
'constraints' => [
Expand Down
5 changes: 2 additions & 3 deletions src/Eccube/Form/Type/Front/NonMemberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Eccube\Form\Type\Front;

use Eccube\Common\EccubeConfig;
use Eccube\Repository\BaseInfoRepository;
use Eccube\Form\Type\AddressType;
use Eccube\Form\Type\KanaType;
use Eccube\Form\Type\NameType;
Expand Down Expand Up @@ -51,9 +52,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('name', NameType::class, [
'required' => true,
])
->add('kana', KanaType::class, [
'required' => true,
])
->add('kana', KanaType::class)
->add('company_name', TextType::class, [
'required' => false,
'constraints' => [
Expand Down
14 changes: 12 additions & 2 deletions src/Eccube/Form/Type/KanaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
namespace Eccube\Form\Type;

use Eccube\Common\EccubeConfig;
use Eccube\Entity\BaseInfo;
use Eccube\Repository\BaseInfoRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand All @@ -22,18 +24,25 @@
class KanaType extends AbstractType
{
/**
* @var \Eccube\Common\EccubeConfig
* @var EccubeConfig
*/
protected $eccubeConfig;

/**
* @var BaseInfo
*/
protected $baseInfo;

/**
* KanaType constructor.
*
* @param EccubeConfig $eccubeConfig
* @param BaseInfoRepository $baseInfoRepository
*/
public function __construct(EccubeConfig $eccubeConfig)
public function __construct(EccubeConfig $eccubeConfig, BaseInfoRepository $baseInfoRepository)
{
$this->eccubeConfig = $eccubeConfig;
$this->baseInfo = $baseInfoRepository->get();
}

/**
Expand All @@ -52,6 +61,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'required' => $this->baseInfo->isOptionRequireKana(),
'lastname_options' => [
'attr' => [
'placeholder' => 'common.last_name_kana',
Expand Down
2 changes: 2 additions & 0 deletions src/Eccube/Resource/template/admin/Customer/edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ file that was distributed with this source code.
<div class="row mb-2">
<div class="col-3">
<span>{{ 'admin.common.kana'|trans }}</span>
{% if BaseInfo.option_require_kana %}
<span class="badge badge-primary ml-1">{{ 'admin.common.required'|trans }}</span>
{% endif %}
</div>
<div class="col">
<div class="row">
Expand Down
14 changes: 12 additions & 2 deletions src/Eccube/Resource/template/admin/Order/edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,12 @@ file that was distributed with this source code.
</div>
</div>
<div class="row form-group">
<label class="col-3 col-form-label">{{ 'admin.common.kana'|trans }}<span class="badge badge-primary ml-1">{{ 'admin.common.required'|trans }}</span></label>
<label class="col-3 col-form-label">
{{- 'admin.common.kana'|trans -}}
{%- if BaseInfo.option_require_kana -%}
<span class="badge badge-primary ml-1">{{ 'admin.common.required'|trans }}</span>
{%- endif -%}
</label>
<div class="col">
<div class="row">
<div class="col">
Expand Down Expand Up @@ -552,7 +557,12 @@ file that was distributed with this source code.
</div>
</div>
<div class="row form-group">
<label class="col-3 col-form-label">{{ 'admin.common.kana'|trans }}<span class="badge badge-primary ml-1">{{ 'admin.common.required'|trans }}</span></label>
<label class="col-3 col-form-label">
{{- 'admin.common.kana'|trans -}}
{%- if BaseInfo.option_require_kana -%}
<span class="badge badge-primary ml-1">{{ 'admin.common.required'|trans }}</span>
{%- endif -%}
</label>
<div class="col">
<div class="row">
<div class="col">
Expand Down
4 changes: 3 additions & 1 deletion src/Eccube/Resource/template/admin/Order/shipping.twig
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,12 @@ file that was distributed with this source code.
</div>
<div class="row form-group">
<label class="col-3 col-form-label">
{{ 'admin.common.kana'|trans }}
{{- 'admin.common.kana'|trans -}}
{%- if BaseInfo.option_require_kana -%}
<span class="badge badge-primary ml-1">
{{ 'admin.common.required'|trans }}
</span>
{%- endif -%}
</label>
<div class="col">
<div class="row">
Expand Down
2 changes: 1 addition & 1 deletion src/Eccube/Resource/template/default/Shopping/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ file that was distributed with this source code.
<dl>
<dt>
<label class="ec-label required">{{ 'common.kana'|trans }}</label>
<span class="ec-required">{{ 'common.required'|trans }}</span>
<span class="{{ BaseInfo.option_require_kana ? 'ec-required' }}">{{ BaseInfo.option_require_kana ? 'common.required'|trans }}</span>
</dt>
<dd>
<div class="ec-halfInput">
Expand Down

0 comments on commit f38f876

Please sign in to comment.