0

I have created a custom attribute to store the users mobile number on registration. It's visible on the registration, and admin page, but upon trying to create a customer I get the following error:

"Mobile Number" is a required value.

Here's the class:

<?php

namespace JR2\ExtraCustomerData\Setup\Patch\Data;

use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UninstallInterface;
use Psr\Log\LoggerInterface;

class AddMobilePhoneAttribute implements DataPatchInterface, UninstallInterface
{
    private $attributeName = 'mobile_number';
    private $moduleDataSetup;
    private $eavSetupFactory;
    private $logger;
    private $eavConfig;
    private $attributeResource;

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

    public function apply()
    {
        $this->moduleDataSetup->getConnection()->startSetup();
        $this->addMobilePhoneAttribute();
        $this->moduleDataSetup->getConnection()->endSetup();
    }

    public function addMobilePhoneAttribute()
    {
        $eavSetup = $this->eavSetupFactory->create([ 'setup' => $this->moduleDataSetup ]);
        $eavSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                $this->attributeName,
                [
                    'type'          => 'varchar',
                    'label'         => 'Mobile Number',
                    'input'         => 'text',
                    'required'      => 1,
                    'visible'       => 1,
                    'user_defined'  => 1,
                    'sort_order'    => 999,
                    'position'      => 999,
                    'system'        => 0
                ]
        );
        
        $attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
        $attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);
        
        $attribute = $this->eavConfig->getAttribute(
                Customer::ENTITY,
                $this->attributeName
        );

        $attribute->setData('attribute_set_id', $attributeSetId);
        $attribute->setData('attribute_group_id', $attributeGroupId);
        $attribute->setData('used_in_forms', [ 'adminhtml_customer', 'customer_account_create' ]);

        $this->attributeResource->save($attribute);
    }
    
    public static function getDependencies()
    {
        return [];
    }

    public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $this->moduleDataSetup->getConnection()->startSetup();
        $eavSetup = $this->eavSetupFactory->create([ 'setup' => $this->moduleDataSetup ]);
        $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, $this->attributeName);
        $this->moduleDataSetup->getConnection()->endSetup();
    }

    public function getAliases()
    {
        return [];
    }
}

And the following phtml template:

<div class="field mobile_number required">
   <label class="label" for="mobile_number">
       <span><?= $block->escapeHtml(__('Mobile Number')) ?></span>
   </label>
   <div class="control">
       <input type="tel" name="mobile_number" id="mobile_number" value="" class="input-text" data-validate="{required:true}">
   </div>
</div>

I've checked the POST data sent with the request and the mobile_number field is definitely being sent. I've checked the docs and can't seem to figure this one out.

3 Answers 3

0
  1. Make required false when create attribute. Change From 'required'=> 1 to 'required'=> 0
$eavSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        $this->attributeName,
        [
            'type'          => 'varchar',
            'label'         => 'Mobile Number',
            'input'         => 'text',
            'required'      => 0,
            'visible'       => 1,
            'user_defined'  => 1,
            'sort_order'    => 999,
            'position'      => 999,
            'system'        => 0
        ]
);
  1. Make required false in html code. from data-validate="{required:true} to data-validate="{required:false} and remove required class
<div class="field mobile_number">
   <label class="label" for="mobile_number">
       <span><?= $block->escapeHtml(__('Mobile Number')) ?></span>
   </label>
   <div class="control">
       <input type="tel" name="mobile_number" id="mobile_number" value="" class="input-text" data-validate="{required:false}">
   </div>
</div>
3
  • But, it's a required parameter so why would I set it as optional? The problem I have is even when I'm sending the mobile_number POST data attribute, the response is: "Mobile Number" is a required value. May 5, 2021 at 16:40
  • I run your code in my setup and it's working fine. Check is there any third-party module which making an issue? try to disable third party module and check and run upgrade, compile command.
    – Mital Shah
    May 6, 2021 at 5:24
  • I reprovisioned my server, added module, and works fine. May 6, 2021 at 11:24
1

For any EAV attribute that is required (like mobile number), you can set it unrequired while creating it or after creation from database table.

  1. While creating it using InstallData.php or UpgradeData.php

    $eavSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,'mobile_number',
            [
                'type'          => 'varchar',
                'label'         => 'Mobile Number',
                'input'         => 'text',
                'required'      => 0,
                'visible'       => 1,
                'user_defined'  => 1,
                'sort_order'    => 150,
                'position'      => 999,
                'system'        => 0
            ]
    );
    
  2. In Eav Attribute table "eav_attribute", search for that attribute "mobile_number" and set required to 0.

0

You have to make Mobile Number as Optional Parameter.
In your code, change required from 1 to 0, see below code.

$eavSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                $this->attributeName,
                [
                    'type'          => 'varchar',
                    'label'         => 'Mobile Number',
                    'input'         => 'text',
                    'required'      => 0,
                    'visible'       => 1,
                    'user_defined'  => 1,
                    'sort_order'    => 999,
                    'position'      => 999,
                    'system'        => 0
                ]
        );

In your PHTML file remove required class and data-validate="{required:true} attribute also. Your code will look like -

<div class="field mobile_number">
   <label class="label" for="mobile_number">
       <span><?= $block->escapeHtml(__('Mobile Number')) ?></span>
   </label>
   <div class="control">
       <input type="tel" name="mobile_number" id="mobile_number" value="" class="input-text">
   </div>
</div>

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.