0

Hello I have one Custom registration filed plugin i want to add field in my custom form how can i do that? i share the code they use customer attribute to add field how can i add field in my custom form

$attributeObj->setData('used_in_forms', $usedInForms);
        $fieldset->addField(
            'used_in_forms',
            'multiselect',
            [
                'name' => 'used_in_forms[]',
                'label' => __('Display Fields in Form'),
                'title' => __('Display Fields in Form'),
                'values' => [
                    ['label' => __('Admin Customer Form'), 'value' => 'adminhtml_customer'],
                    ['label' => __('Customer Account'), 'value' => 'customer_account_edit'],
                    ['label' => __('Registration Form'), 'value' => 'customer_account_create'],

                    ['label' => __('Custom Registration Form'), 'value' => 'customer_index_index'],
                ],
                'required' => true
            ]
        );
1
  • you want to add a custom field in registration or static or dynamic field Commented Aug 12, 2019 at 7:20

1 Answer 1

1

app/code/Cm/Customer/Setup/InstallData.php

<?php
namespace Cm\Customer\Setup;

use Magento\Customer\Model\Customer;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements \Magento\Framework\Setup\InstallDataInterface
{
    private $eavSetupFactory;

    private $eavConfig;

    private $attributeResource;

    public function __construct(
        \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
        \Magento\Eav\Model\Config $eavConfig,
        \Magento\Customer\Model\ResourceModel\Attribute $attributeResource
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
        $this->attributeResource = $attributeResource;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->removeAttribute(Customer::ENTITY, "skype");

        $attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
        $attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);

        $eavSetup->addAttribute(Customer::ENTITY, 'skype', [
            // Attribute parameters
            'type' => 'varchar',
            'label' => 'Skype Account',
            'input' => 'text',
            'required' => true,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 990,
            'position' => 990,
            'system' => 0,
        ]);

        $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'skype');
        $attribute->setData('attribute_set_id', $attributeSetId);
        $attribute->setData('attribute_group_id', $attributeGroupId);

        /*
        //You can use this attribute in the following forms
        adminhtml_checkout
        adminhtml_customer
        adminhtml_customer_address
        customer_account_create
        customer_account_edit
        customer_address_edit
        customer_register_address
        */

        $attribute->setData('used_in_forms', [
            'adminhtml_customer',
            'customer_account_create',
            'customer_account_edit'
        ]);

        $this->attributeResource->save($attribute);
    }
}
?>

app/code/Cm/Customer/view/frontend/templates/additional.phtml

<div class="field skype required">
    <label class="label" for="skype">
      <span><?= $block->escapeHtml(__('Skype Account')) ?></span>
    </label>
    <div class="control">
       <input type="text" name="skype" id="skype" value="" title="<?= $block->escapeHtmlAttr(__('Skype Account')) ?>" class="input-text" data-validate="{required:true}">
    </div>
</div>

app/code/Cm/Customer/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="form_additional_info_customer" template="Rsgitech_Customer::additional.phtml"/>
        </referenceContainer>
    </body>
</page>

you want to add a custom field as static refer this lik

otherwise you want to add custom field as dynamic field use this extension

1
  • I have already one Extension i want to include those field in my custom registration form not in magento default customer form @divya sekar Commented Aug 12, 2019 at 7:47

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.