1

I manage to add my custom customer attribute with InstallData on my custom module it appears everything is working fine, but when i try save or edit customer in front end or admin panel, when i click on the Save Button when editing a customer or creating a customer i got this error. I'm on Magento V 2.3.2

Fatal error: Uncaught TypeError: Argument 2 passed to Magento\Eav\Model\Attribute\Data\Text::validateLength() must be of the type string, null given vendor/magento/module-eav/Model/Attribute/Data/Text.php on line 141.

Here is the InstallData I used to create the attribute.

<?php

namespace Kmdelectronics\Customer\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;
use Magento\Customer\Model\Customer;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    private $eavConfig;

    public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig       = $eavConfig;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'cedula_attribute',
            [
                'type'         => 'varchar',
                'label'        => 'Cedula',
                'input'        => 'text',
                'required'     => true,
                'visible'      => true,
                'user_defined' => true,
                'position'     => 999,
                'system'       => 0,
            ]
        );
        $sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'cedula_attribute');

        // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
        $sampleAttribute->setData(
            'used_in_forms',
                [
                'adminhtml_customer',
                'customer_account_create',
                'customer_account_edit'
                ]);
        $sampleAttribute->save();
    }
}
2
  • It means that your attribute doesn't created properly Please share code which you have used to create customer attribute.
    – anonymous
    Commented Jul 15, 2019 at 7:17
  • Hi, @Vivek thanks for your answer, i just added the code I used to create my attribute, also i editted the position directly from the db to change it to 39 because i want to display it before the name Commented Jul 15, 2019 at 12:10

1 Answer 1

0

I found the solution, the problem was because I didn't add the values attribute_set_id and attribute_group_id to my attribute if someone else have the same problem is here the UpgradeData method I create to solve my problem.

Also, can be the same problem if your attribute is required: false, your form will be ok but the attribute never will go to the db.

public function upgrade(ModuleDataSetupInterface $setup,
ModuleContextInterface $context) {
        if (version_compare($context->getVersion(), '1.0.3', "<")) {
            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
            $setup->startSetup();
            $attributesInfo = [
                'cedula' => [
                    'label' => 'Cedula',
                    'type' => 'varchar',
                    'input' => 'text',
                    'position' => 38,
                    'visible' => true,
                    'required' => true,
                    'system' => 0,
                    'user_defined' => true,
                    'is_used_in_grid' => true,
                    'is_visible_in_grid' => true,
                    'is_html_allowed_on_front' => true,
                    'visible_on_front' => true
                ]
            ];
            $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
            $attributeSetId = $customerEntity->getDefaultAttributeSetId();

            $attributeSet = $this->attributeSetFactory->create();
            $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
            foreach ($attributesInfo as $attributeCode => $attributeParams) {
                $customerSetup->addAttribute(Customer::ENTITY, $attributeCode, $attributeParams);
            }
            $Attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'cedula');
            $Attribute->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer',
                'customer_account_create',
                'customer_account_edit'],
            ]);
            $Attribute->save();


        }
        $setup->endSetup();
    }

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.