Magento – Create customer custom attribute with input type checkbox, multiselect, and radio Magento 2

custom-attributescustomer-attributeinputmagento2

I successfully created custom attribute for customer with input type select/dropdown like this:

$code = 'test';
$customerSetup = $this->customerSetupFactory->create();
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, $code);
$insertData = array(
        "type"     => "int",
        "backend"  => "",
        "label"    => 'test',
        "input"    => 'select',
        "source"   => "",
        "visible"  => true,
        "required" => false,
        "default" => "",
        "frontend" => "",
        "unique"     => false,
        "note"       => ""
      );

$insertData['option']['values'] = array(0 => 'No', 1 => 'Yes');
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, $code,  $insertData);
$attribute   = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, $code);

$attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, $code);
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$attribute->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", 11);

$attribute->save();

the problem is when i try to change the input type to checkbox "input"=>"checkbox", the custom attribute is saved but the values option do not appear when i try to add new customer via admin , here's how it looks:
enter image description here

and i need to add more custom attribute with input type radio and multiselect, but i havent tried it, i'm afraid it will be like the checkbox case

Best Answer

For Checkbox type customer attribute, Please change the following:

    "type"     => "int",
    "backend"  => "",
    "label"    => "Test",
    "input"    => "boolean",
    "source"   => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
    "visible"  => true,
    "required" => true,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => ""

Hope this will help you.