9

I want to add a custom attribute in customer registration form. I write a module with the following InstallData.php

<?php

namespace vendor\TestModule\Setup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface {

    /**
     * Customer setup factory
     *
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;

    public function __construct(CustomerSetupFactory $customerSetupFactory) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
        $setup->startSetup();
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(Customer::ENTITY, 'client_idn', [
            'label' => 'Client IDN',
            'input' => 'text',
            'required' => false,
            'sort_order' => 40,
            'visible' => true,
            'system' => false,
            'is_used_in_grid' => true,
            'is_visible_in_grid' => true,
            'is_filterable_in_grid' => true,
            'is_searchable_in_grid' => true]
        );

        // add attribute to form
        /** @var  $attribute */
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'client_idn');
        $attribute->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create']);
        $attribute->save();

        $setup->endSetup();
    }

}

In database, the records are successfully inserted. enter image description here enter image description here

However, the custom attribute does not show up in the registration form. enter image description here

Is there anything wrong?

6 Answers 6

11

If your install data script successfully installed your custom attribute, now you just need to override addition information phtml file and set your custom attribute in that.

Create Vendor/TestModule/view/frontend/layout/customer_account_create.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="form.additional.info">
        <block class="Magento\Framework\View\Element\Template" name="additional_info_customer_client_idn" template="Vendor_TestModule::additionalinfocustomer.phtml"/>
    </referenceContainer>
</body>
</page>

Create Vendor/TestModule/view/frontend/templates/additionalinfocustomer.phtml

<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>">
    <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Additional Information') ?></span></legend>
    <p>
    <div class="field regulation">
        <label for="regulation" class="label"><span><?php /* @escapeNotVerified */
                echo __('Client IDN') ?></span></label>
        <div class="control">
            <input type="text" name="client_idn" id="client_idn" title="<?php /* @escapeNotVerified */ echo __('Client IDN') ?>" class="input-text" data-validate="{required:false}">
        </div>
    </div>
    </p>
</fieldset>

Clear cache and review frontend customer account registration page.

2
  • 3
    Vendor/TestModule/view/layout/customer_account_create.xml should be Vendor/TestModule/view/frontend/layout/customer_account_create.xml Commented Oct 18, 2016 at 11:28
  • Hello... what to do to assign the value to the INPUT of the custom attribute? The problem is| that if the page is reloaded because of an error, all fields are repopulated with what the user entered in the form, except the custom fields.
    – jstuardo
    Commented Jul 23, 2021 at 14:16
1

@Jonak's answer "the most voted" is correct, however you need to create the layout folder inside frontend folder just like that:

from:

Vendor/TestModule/view/layout/customer_account_create.xml

to:

Vendor/TestModule/view/frontend/layout/customer_account_create.xml

0

It should be added in front-end or html level.

Assume you are using Luma theme

Go to customer registration form root/vendor/magento/module-customer/view/frontend/templates/form/register.phtml add below code

<div class="field required">
    <label for="client_idn" class="label"><span><?php /* @escapeNotVerified */ echo __('Client Idn') ?></span></label>
    <div class="control">
        <input type="client_idn" name="client_idn" id="client_idn" value="<?php echo $block->escapeHtml($block->getFormData()->getClientIdn()) ?>" title="<?php /* @escapeNotVerified */ echo __('client_idn') ?>" class="input-text" data-validate="{required:true}">
    </div>
    </div>

Remove the cache and check the visibility in front end.

Check whether the value is passing to customer registration controller or not.

@Aaron Allen said in comment override the file by module. editing code in core files is not recommended.

Hope this helps.

5
  • Override the file with your module instead. Editing a core file is bad. Commented Jul 30, 2016 at 19:29
  • @ Aaron Allen Thanks, i forgot to mention, modified the answer. Commented Jul 30, 2016 at 20:33
  • @krishnaijjada, but why this? <?php echo $block->escapeHtml($block->getFormData()->getEmail()) ?>
    – TFS
    Commented Jul 31, 2016 at 7:38
  • it renders data from session if it is already set. $formData = $this->_customerSession->getCustomerFormData(true); Commented Jul 31, 2016 at 8:01
  • but data is not saving from frontend Commented Feb 21, 2017 at 10:16
0

you can checkout an excellent tutorial link for creating custom attribute for Customer. This also contains video.
Follow the steps. It works.
http://www.ibnab.com/en/blog/magento-2/magento-2-add-custom-eav-attribute-to-category-or-customer
Hope this helps.

0

It happen because your entity in

$customerSetup->getEavConfig()->getAttribute('customer', 'client_idn');

is

Magento\Eav\Model\ResourceModel\Entity\Attribute

But saving form data exist only in

Magento\Eav\Model\ResourceModel\Attribute

(have a look at function _afterSave)

This is not best practice, but solution:

        $attribute = $this->attributeFactory->create();
        $attribute->loadByCode($attributeArray['type'], $attributeArray['code']);
        $attribute->setData('used_in_forms', $attributeForms);
        $attribute->save();

where AttributeFactory is: Magento\Customer\Model\AttributeFactory; attribute forms are:

    $attributeForms = [
        'customer_account_create',
        'customer_address_edit',
        'customer_register_address'
    ];

attribute array:

    [
        'type' => 'customer_address',
        'code' => 'title',
    ]
0

If your install data script successfully installed your custom attribute, now you just need to override addition information phtml file and set your custom attribute in that.

Create vendor/TestModule/view/frontend/layout/customer_account_create.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_form_login">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">vendor_TestModule::register.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

Create vendor/TestModule/view/frontend/templates/register.phtml

<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>">
    <div class="field client idn">
        <label for="client_idn" class="label">
            <span>
                <?php /* @escapeNotVerified */echo __('Client IDN') ?>
            </span>
        </label>
        <div class="control">
            <input type="text" name="client_idn" id="client_idn" title="<?php /* @escapeNotVerified */ echo __('Client IDN') ?>" class="input-text" data-validate="{required:true}">
        </div>
    </div>
</fieldset>

Clear cache & review frontend customer account registration page...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.