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();
}
}