0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

EC-CUBE4 会員登録フォームの入力を最小限にする

Last updated at Posted at 2021-01-22

FormExtension を利用して、フォーム定義を更新

:warning: required 属性を false にして、さらに constraints をクリアします。

app/Customize/Form/Extension/EntryFormExtension.php
<?php

declare(strict_types=1);

namespace Customize\Form\Extension;

use Eccube\Form\Type\AddressType;
use Eccube\Form\Type\Front\EntryType;
use Eccube\Form\Type\KanaType;
use Eccube\Form\Type\PhoneNumberType;
use Eccube\Form\Type\PostalType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;

class EntryFormExtension extends AbstractTypeExtension
{
    public function getExtendedType(): string
    {
        return EntryType::class;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $this->disableRequired($builder);
    }

    private function disableRequired(FormBuilderInterface $builder): void
    {
        $fieldNames = [
            'kana' => KanaType::class,
            'postal_code' => PostalType::class,
            'address' => AddressType::class,
            'phone_number' => PhoneNumberType::class,
        ];

        foreach ($fieldNames as $fieldName => $type) {
            $field = $builder->get($fieldName);
            $options = $field->getOptions();
            $options['required'] = false;
            $options['constraints'] = [];
            $builder->add($fieldName, $type, $options);
        }
    }
}

入力画面の修正1 - 任意入力の項目全てが空送信されるようにする

修正前(30行目あたり)

app/template/your_template_code/Entry/index.twig
{{ form_widget(form._token) }}

修正後

app/template/your_template_code/Entry/index.twig
{{ form_widget(form._token) }}
{{ form_widget(form.kana.kana01, {'type': 'hidden'}) }}
{{ form_widget(form.kana.kana02, {'type': 'hidden'}) }}
{{ form_widget(form.company_name, {'type': 'hidden'}) }}
{{ form_widget(form.postal_code, {'type': 'hidden'}) }}
{{ form_widget(form.address.pref, {'type': 'hidden'}) }}
{{ form_widget(form.address.addr01, {'type': 'hidden'}) }}
{{ form_widget(form.address.addr02, {'type': 'hidden'}) }}
{{ form_widget(form.phone_number, {'type': 'hidden'}) }}
{{ form_widget(form.birth.year, {'type': 'hidden'}) }}
{{ form_widget(form.birth.month, {'type': 'hidden'}) }}
{{ form_widget(form.birth.day, {'type': 'hidden'}) }}
{{ form_widget(form.sex, {'type': 'hidden'}) }}
{{ form_widget(form.job, {'type': 'hidden'}) }}

入力画面の修正2 - フィールドの削除

任意入力の項目は、前項で空送信されるように変更したので、以下のようなコードを削除

<dl>
    <dt>
        {{ form_label(form.kana, 'common.kana', { 'label_attr': { 'class': 'ec-label' }}) }}
    </dt>
    <dd>
        <div class="ec-halfInput{{ has_errors(form.kana.kana01, form.kana.kana02) ? ' error'}}">
            {{ form_widget(form.kana.kana01, { 'attr': { 'placeholder': 'common.last_name_kana' }}) }}
            {{ form_widget(form.kana.kana02, { 'attr': { 'placeholder': 'common.first_name_kana' }}) }}
            {{ form_errors(form.kana.kana01) }}
            {{ form_errors(form.kana.kana02) }}
        </div>
    </dd>
</dl>

確認画面の修正

入力画面と同じような修正を app/template/your_template_code/Entry/confirm.twig にします。

完成 :raised_hands_tone2:

スクリーンショット 2021-01-22 11.40.18.png

:warning: 注意事項

ただし、この修正を行うと、カート画面の「レジに進む」(PurchaseFlowの送料明細追加)でエラーになります。

スクリーンショット 2021-01-28 11.02.12.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?