I am using below code for adding customer attribute in admin form with some required value.
$installer = $setup;
$installer->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "company_name", array(
"type" => "varchar",
"backend" => "",
"label" => "Company Name",
"input" => "text",
"source" => "",
"visible" => true,
"required" => true,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$company_name = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "company_name");
$company_name = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'company_name');
$used_in_forms[]="adminhtml_customer";
$company_name->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 100);
$company_name->save();
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "company_registration", array(
"type" => "varchar",
"backend" => "",
"label" => "Company Registration",
"input" => "text",
"source" => "",
"visible" => true,
"required" => true,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$company_registration = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "company_registration");
$company_registration = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY,'company_registration');
$used_in_forms = array();
$used_in_forms[]="adminhtml_customer";
$company_registration->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 110);
$company_registration->save();
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "gst_no", array(
"type" => "varchar",
"backend" => "",
"label" => "Gst No.",
"input" => "text",
"source" => "",
"visible" => true,
"required" => true,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$gst_no = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "gst_no");
$gst_no = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY,'gst_no');
$used_in_forms = array();
$used_in_forms[]="adminhtml_customer";
$gst_no->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 110);
$gst_no->save();
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "company_address", array(
"type" => "varchar",
"backend" => "",
"label" => "Company Address",
"input" => "text",
"source" => "",
"visible" => true,
"required" => true,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$company_address = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "company_address");
$company_address = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY,'company_address');
$used_in_forms = array();
$used_in_forms[]="adminhtml_customer";
$company_address->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 110);
$company_address->save();
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "contact_number", array(
"type" => "varchar",
"backend" => "",
"label" => "Company Number",
"input" => "text",
"source" => "",
"visible" => true,
"required" => true,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$contact_number = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "contact_number");
$contact_number = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY,'contact_number');
$used_in_forms = array();
$used_in_forms[]="adminhtml_customer";
$contact_number->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 110);
$contact_number->save();
$installer->endSetup();
But It gives me error of required values in registration form. I don't want these fields in registration forms.
Please help me to solve this.