You need to InstallData.php like below code :
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$this->logger->info("Username data installed.");
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(Customer::ENTITY, 'username', [
'type' => 'varchar',
'label' => 'Username',
'input' => 'text',
'backend' => \Vendor\Module\Model\Attribute\Backend\Username::class,
'required' => false,
'visible' => true,
'user_defined' => true,
'unique' => true,
'required' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'username')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer', 'checkout_register', 'customer_account_create', 'customer_account_edit', 'adminhtml_checkout'],
]);
$attribute->save();
}
Please create Username.php file in \Vendor\Module\Model\Attribute\Backend and code like
<?php
namespace Vendor\Module\Model\Attribute\Backend;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
class Username extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
{
/**
* Generate and set unique Username to customer
*
* @param Customer $object
* @return void
*/
protected function checkUniqueUsername($object)
{
$attribute = $this->getAttribute();
$entity = $attribute->getEntity();
$attributeValue = $object->getData($attribute->getAttributeCode());
$increment = null;
while (!$entity->checkAttributeUniqueValue($attribute, $object)) {
throw new NoSuchEntityException(__('Account with Username is already exist'));
}
}
/**
* Make username unique before save
*
* @param Customer $object
* @return $this
*/
public function beforeSave($object)
{
$this->checkUniqueUsername($object);
return parent::beforeSave($object);
}
}
I have solved it. I rewrite controller Magento\Customer\Controller\Account\CreatePost
my di.xml
code as below:
<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Controller\Account\CreatePost" type="Vendor\Modulename\Controller\Account\CreatePost" />
</config>
Add below code in controller file code Vendor\Modulename\Controller\Account\CreatePost.php
$customer->setCustomAttribute('test',$this->getRequest()->getPost('test'));
Best Answer
Please Try below code :