1

I want to make one of customer attributes not required

I run the below code, but it doesn't work

   public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $customerSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'trade_license',
            [
                'required' => false
            ]
        );
}

2 Answers 2

2

You can use the following code to update customer attribute

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    // Define the attribute code
    $attributeCode = 'trade_license';

    // Update the attribute
    $customerSetup->updateAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        $attributeCode,
        'is_required',
        false
    );

    // Remove any validation rules that make it required
    $customerSetup->updateAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        $attributeCode,
        'validate_rules',
        ''
    );
}
0

I think addAttribute() is not for the update the attribute

you can follow this link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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