cancel
Showing results for 
Search instead for 
Did you mean: 

Customer custom attribute with checkbox input throws exception on admin save

Customer custom attribute with checkbox input throws exception on admin save

I created an InstallData class to add two custom attributes to the Customer. One has input type 'text' and the other has input type 'checkbox'. The code to create the checkbox type is listed below.

 

 

        $customerSetup->addAttribute(Customer::ENTITY, 'customer_approved', [
            'type' => 'int',
            'label' => 'Customer Approved',
            'input' => 'checkbox',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1001,
            'position' => 1001,
            'system' => 0,
        ]);
        
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_approved')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],
        ]);
        
        $attribute->save();

The attribute displays a checkbox properly on the admin customer edit page, but saving the customer does not work and in debug there is an exception and stack trace that begins:

 

Class Magento\Customer\Model\Metadata\Form\Checkbox does not exist
#0 /vagrant/html/vendor/magento/framework/Code/Reader/ClassReader.php(19): ReflectionClass->__construct('Magento\\Custome...')

 

If I change the input type to 'text' and enter a value like 1 or 0 the save works.

 

Is 'checkbox' the wrong type to use for input? Is 'int' the wrong type to use for a boolean? Or is this a Magento bug?

 

7 REPLIES 7

Re: Customer custom attribute with checkbox input throws exception on admin save

Hi,

you should use 

'input' => 'boolean',

 

Re: Customer custom attribute with checkbox input throws exception on admin save

When I tried setting frontend_input to 'boolean', the int value 1 was rendered as a checked box. I was able to clear the checkbox and save successfully. However, when I checked the checkbox again and saved, the save appeared to complete without errors, but the value was not updated. So there is still a bug, even with input -> 'boolean'.

 

I tried this with 2.0.6.

 

 

Re: Customer custom attribute with checkbox input throws exception on admin save

You can use "Catalog Input Type for Store Owner" = "Yes/No" .

Re: Customer custom attribute with checkbox input throws exception on admin save

This worked for me. Thanks

Here is my code

$eavSetup->addAttribute(
            Product::ENTITY,
            'free_shipping',
            [
                'group' => 'Product Details',
                'type' => 'int',
                'backend' => '',
                'frontend' => '',
                'label' => 'Free Shipping',
                'note' => 'Are we offering free shipping on this product?',
                'input' => 'boolean',
                'class' => '',
                'source' => '',
                'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => '0',
                'searchable' => true,
                'filterable' => true,
                'comparable' => true,
                'visible_on_front' => true,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => '',
                'sort_order' => 11
            ]
        );

Re: Customer custom attribute with checkbox input throws exception on admin save

If the checkbox contains more than 3 options, then how the boolean type is work

Re: Customer custom attribute with checkbox input throws exception on admin save

Hi @dnadle @jayashree_n1

 

Please check below code for select multiple option value. I hope it is helpful for you

$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_approved', [
                'type' => 'varchar',
                'label' => 'Customer Approved',
                'input' => 'multiselect',
                'source' => 'Yourcompany\Customer\Model\Customer\Attribute\Source\CustomerApproved',
                'required' => false,
                'visible' => true,
                'position' => 333,
                'system' => false,
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend'
            ]);
If you find my answer useful, Please click Kudos & Accept as Solution.

Thanks Smiley Happy

Re: Customer custom attribute with checkbox input throws exception on admin save

it is given dropdown option not checkbox