1

In magento 2.3, I have created custom field(GST number) on registration form. This field's value is saving on DB as. This field's value is showing/editing on admin panel(customer edit). But not showing in customer's account information tab. As i try to update first or last name and click on save button then hey show validation message:

GST number is required value

.

There is my script:

<?php
namespace Test\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, "gst_number");

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

        $eavSetup->addAttribute(Customer::ENTITY, 'gst_number', [
            // Attribute parameters
            'type' => 'varchar',
            'label' => 'GST number',
            'input' => 'text',
            'required' => true,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 990,
            'position' => 990,
            'system' => 0,
        ]);
        
        $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'gst_number');
        $attribute->setData('attribute_set_id', $attributeSetId);
        $attribute->setData('attribute_group_id', $attributeGroupId);

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

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

enter image description here

1
  • Hey, anybody have can help me into this? – vijay gupta Nov 13 '20 at 16:57
0

You have to override edit.phtml in your theme or in your module you can copy from this location and add field according to your need -

/magentoroot/vendor/magento/module-customer/view/frontend/templates/form/edit.phtml

add this code above checkbox -

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

Please check image too right now i add in my localhost for you.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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