7

I have created custom customer attribute and use at registration process. Its working fine.

Now I want update attribute like - previously its 'required'=> false. Now I want to set it as 'required'=> true.

But don't know how to upgrade it using UpgradeData.php

Plz help to solve this.

I tried with below code -

if (version_compare($context->getVersion(), "1.0.6", "<")) 
        {
            /** @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->updateAttribute(\Magento\Customer\Model\Customer::ENTITY, 'netz_allowed', [
                'visible_on_front' => 1,
                'input' => 'select',
                'require' => true,          
                'source' => 'Magento\Config\Model\Config\Source\Yesno',
            ]);

            $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'netz_allowed')
            ->addData(['used_in_forms' => ['customer_account_create','customer_account_edit']]);

            $attribute->save(); 
        }

        $setup->endSetup();

3 Answers 3

7

Try this

<?php
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
    /** @var CustomerSetup $customerSetup */
       $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

       $customerSetup->updateAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                'your_attribute_id',                    
                'required',
                true
            );


          $Attribute = $customerSetup->getEavConfig()->getAttribute( \Magento\Customer\Model\Customer::ENTITY, 'your_attribute_id');

        $Attribute->setData(
        'used_in_forms',
            [
            //  'adminhtml_customer_address',  //select if need
            //  'customer_address_edit',       //select if need
            //  'customer_register_address'    //select if need
            ]
    );
        $Attribute->save();

        $setup->endSetup();
    }

    ?>

use above code in your script

6
  • Thanks nikhil, As we set 'used_in_forms', "is there any way to unset from forms" ?
    – Atul
    Mar 6, 2017 at 12:41
  • I tried your way and the above one also - but non making effect. Please check in updated question.
    – Atul
    Mar 6, 2017 at 12:54
  • Above attribute currently showing in admin's customer form, but now I don't want.
    – Atul
    Mar 6, 2017 at 12:58
  • Hi Please check update code Mar 6, 2017 at 13:02
  • its giving this error - [Zend_Db_Statement_Exception] SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (csep.eav_attribute, CONSTR AINT EAV_ATTRIBUTE_ENTITY_TYPE_ID_EAV_ENTITY_TYPE_ENTITY_TYPE_ID FOREIGN KEY (entity_type_id) REFERENCES eav_entity_type (entity_type_id) ON D ELETE CASCAD), query was: INSERT INTO eav_attribute () VALUES ()
    – Atul
    Mar 7, 2017 at 9:37
3
 public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '0.0.5', '<')) {
            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
            $customerSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                'lastname',
                [
                    'required' => false
                ]
            );
        }
2

In my case, this worked for me:

if (version_compare($context->getVersion(), '0.0.9', '<')) {
    $customerSetup->updateAttribute(\Magento\Customer\Model\Customer::ENTITY, 'your_attribute_id', ['required' => true]);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.