0

I am creating a new attribute (mobile) for the Customer Entity. The attribute should be of type 'static' since this one is a required field and for easy retrieval.

I came to know that it requires 2 steps:

  1. Create a column in the customer_entity table.
  2. Add an attribute to the customer entity with Patch.

Below is my code:

  1. Creating column in customer_entity table. db_schema.xml
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="customer_entity">
        <column xsi:type="varchar" name="mobile" comment="Customer Mobile Number" nullable="false"/>
    </table>
</schema>

Generated scheme whitelist db_schema_whitelist.json:

{
    "customer_entity": {
        "column": {
            "mobile": true
        }
    }
}
  1. Created a Data Patch for adding a new attribute in the eav_attribute table, AddMobileAttributePatch.php:
public function apply()
    {
        $this->moduleDataSetup->startSetup();

        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $customerSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'mobile',
            [
                'type'         => 'static',
                'label'        => 'Mobile Number',
                'input'        => 'text',
                'required'     => true,
                'unique'       => true,
                'visible'      => true,
                'user_defined' => true,
                'position'     => 999,
                'system'       => 0,
            ]
        );

        $attribute = $customerSetup->getEavConfig()
                                    ->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'mobile')
                                    ->addData([
                                        'used_in_forms' => [
                                            'adminhtml_customer',
                                            'checkout_register',
                                            'customer_account_create',
                                            'customer_account_edit'
                                        ],
                                    ]);

        $attribute->save();

        $this->moduleDataSetup->endSetup();
    }

Now the column is created in the customer_entity table and a new attribute is available in the eav_attribute table. The field is also available in the Admin Customer Edit Form.

The problem is when I enter some data into the mobile field and hit save, it returns the error " Mobile Number" is a required value.. This error is from the server-side and Js validation is working fine.

Can anyone help me get through this? Magento-customer-entity-attribute-required-error

7
  • 1
    The error comes from the admin or frontend? Commented Aug 9, 2021 at 8:30
  • It is from the admin end. @DhirenVasoya
    – Harish ST
    Commented Aug 9, 2021 at 11:14
  • instead of mobile please try to change attribute code "mobileno" Commented Aug 9, 2021 at 12:56
  • @GohilRajesh Is there any attribute named "mobile" in Customer Entity already?
    – Harish ST
    Commented Aug 9, 2021 at 13:15
  • I think not but please try once by changing name. that will make us more sure Commented Aug 10, 2021 at 5:06

1 Answer 1

0

tl;dr: Update is_user_defined and system property to false. This is for my specific where I do not want the attribute to be a system value.

The issue was related to below issues: Customer attribute gives error of required value #7753 Customer EAV Decimal Attribute required allow 0 #20030

During debugging, when the validateValue($value) method was called in Magento\Eav\Model\Attribute\Data\Text, I was getting empty string for $value. But the $_POST does have the newly created attribute value.

Then luckily I found this thread. Attribute property 'system' - what it exactly does

\Magento\Customer\Model\Metadata\CustomerMetadata::getCustomAttributesMetadata These methods are deciding to add or not to add your created attributes to custom_attributes array. It means that these attributes will not be accessible for CRUD operations in Repositories.

Then I changed the is_user_defined property to false and now the attribute value is getting saved in the DB.

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.