Magento 2 – How to Remove Mandatory Validation for Customer Fields

admin-paneleav-attributesmagento2

I want to remove the mandatory validation for customer fields eg. firstname, lastname (both for frontend and admin).

I tried using UpgradeData to change the value of is_required field to 0 in the eav_attribute table.

This changes the required condition in the customer logged in page as not required for firstname and lastname.

But in the admin panel while adding new customer, the firstname field goes not required, whereas the lastname field remains required.

What can I do to remove the required field in the lastname in admin panel.

Admin Panel With lastname as required

UpgradeData.php

class UpgradeData implements UpgradeDataInterface
{
    public function __construct(
        EavSetup $eavSetupFactory
    )
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
        $setup->startSetup();
        if (version_compare($context->getVersion(), '1.0.1', '<')) {
            $this->eavSetupFactory->updateAttribute(1,5,'is_required',0,null);
            $this->eavSetupFactory->updateAttribute(2,23,'is_required',0,null);
        
            $this->eavSetupFactory->updateAttribute(1,7,'is_required',0,null);
        
            $this->eavSetupFactory->updateAttribute(2,25,'is_required',0,null);
         }
         $setup->endSetup();

}

FirstName is_required as 0 in db
FirstName is_required as 0

LastName is_required as 0 in db
LastName is_required as 0

Best Answer

If you want to remove require entry for admin you have replace 1 to 0 in "eav_attribute" on particular "attribut_code"enter image description here

and after that, you have also to comment or remove

<validation> <rule name="required-entry" xsi:type="boolean">true</rule> </validation>

from: vendor\magento\module-customer\view\base\ui_component\customer_form.xml (override customer_form.xml file in your module) enter image description here

Related Topic