How to add a custom field to the frontend registration form (a ready example)

form
customer
layout
customer-registration
(Mateusz Procner) #1

Hi,
I have a problem with adding new field in customer create account form. Field shows properly in admin panel add new customer form, but not in frontend create account. I created module like this:

MyModules\CustomerMarketingFields\Setup\InstallData.php

<?php
namespace MyModules\CustomerMarketingFields\Setup;
use Magento\Framework\Module\Setup\Migration;
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;
    /**
     * Init
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }
    /**
     * Installs DB schema for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
    $installer = $setup;
    $installer->startSetup();
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    $entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);

    $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "regulation",  array(
        "type"     => "int",
        "backend"  => "",
        "label"    => "Regulation",
        "input"    => "boolean",
        "source"   => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
        "visible"  => true,
        "required" => true,
        "default" => "",
        "frontend" => "",
        "unique"     => false,
        "note"       => ""

    ));

    $regulation   = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "regulation");

    $regulation = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'regulation');
    $used_in_forms[]="adminhtml_customer";
    $used_in_forms[]="checkout_register";
    $used_in_forms[]="customer_account_create";
    $used_in_forms[]="customer_account_edit";
    $used_in_forms[]="adminhtml_checkout";
    $regulation->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100);
    $regulation->save();

    $installer->endSetup();
}

}

MyModules\CustomerMarketingFields\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="my_form_additional_info_customer" template="MyModules_CustomerMarketingFields::additionalinfocustomer.phtml"/>
    </referenceContainer>
</body></page>

MyModules\CustomerMarketingFields\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 required">
    <label for="regulation" class="label"><span><?php /* @escapeNotVerified */
            echo __('Regulation') ?></span></label>
    <div class="control">
        <input type="checkbox" checked name="regulation" id="regulation" title="<?php /* @escapeNotVerified */
        echo __('Regulation') ?>" class="input-text" data-validate="{required:true}">
    </div>
</div>
</p></fieldset>

Files module.xml and registration.php are not included here. Could anyone tell me what I am doing wrong?

Thank You in advance.

0 Likes

How to add a custom field to the frontend registration form?
How do built-in Magento modules add a custom block to the frontend customer registration form?
(Dmitry Fedyuk) #2

Is the MyModules\CustomerMarketingFields\View\Frontend\templates\additionalinfocustomer.phtml file executed by the PHP interpreter?

0 Likes

(Mateusz Procner) #3

No it isn’t.

0 Likes

(Dmitry Fedyuk) #4

Is your module loaded by Magento?

0 Likes

(Mateusz Procner) #5

Yes, field in admin panel create customer form is properly added.

0 Likes

(Dmitry Fedyuk) #6

Your MyModules\CustomerMarketingFields\View\Frontend\layout\customer_account_create.xml path is wrong:
view and frontend folder names should be lowercased.

1 Like

(Mateusz Procner) #7

It is showing now. Thanks a lot for help!

0 Likes

(Jorbel Borrero) #8

I tried use this example and I see that field in the registration form in the frontend but I don’t see that en the mg_eav_attribute table or in the backend, maybe lack something

0 Likes

(Jorbel Borrero) #9

am sorry, I did reinstalled magento and now I can see the field in the eav_attribute table, but doesn’t save the value, how I can do it?

0 Likes

(Mateusz Procner) #10

values are stored in customer_entity_* table

    • depends what type of field You add (customer_entity_int for me)
0 Likes

(Harish Garg) #11

hii Mateusz Procner , this is working nice ,but don’t know the table name in database where custom field stored .

0 Likes

(Jarnail S) #12

Hey there, I am already able to add 1 filed in front and admin. But I need to add around 10 fields and one textarea field. can you please give some idea to do that?

0 Likes

(Gamma Testing) #13

Hello i have issue with checkbox validation when checkbox required field. No error message display.

<div class="field regulation required">
    <label for="regulation" class="label"><span><?php /* @escapeNotVerified */
            echo __('Regulation') ?></span></label>
    <div class="control">
        <input type="checkbox" checked name="regulation" id="regulation" title="<?php /* @escapeNotVerified */
        echo __('Regulation') ?>" class="input-text" data-validate="{required:true}">
    </div>
</div>
0 Likes

(Rajesh Nagappan) #14

i’m also using this module i have one error when submit registration data it show error msg “con’t save the custoer” please any one help me

0 Likes

(Jayaseelan T) #15

I added mobile number as custom field to frontend registration as per this example. So i can add mobile number to my customer registration form and saved in db. but in edit account information page, i need to set & get mobile number field. here, we placed our block template in form_additional_info container. so, how to set & get value for mobile number field?

0 Likes

(Dmitry Fedyuk) #16

How do built-in Magento modules add a custom block to the frontend customer registration form?

0 Likes