0

I have created custom module to create custom customer attribute and its working fine for all type i.e select,file.. but not saving the value like email type.

below is the code to create attribute

       $attributeCode = 'referred_email';

        $customerSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY, $attributeCode, [
            'type' => 'static',
            'label' => 'Referral Email',
            'input' => 'text',
            'source' => '',
            'required' => false,
            'visible' => true,
            'position' => 201,
            'validate_rules' =>'{"input_validation":"email"}',
            'system' => false,
            'backend' => ''
                ]
        );

        // show the attribute in the following forms
        $attribute = $customerSetup
                ->getEavConfig()
                ->getAttribute(
                        \Magento\Customer\Model\Customer::ENTITY, $attributeCode
                )
                ->addData(
                ['used_in_forms' => [
                        'adminhtml_customer',
                        'adminhtml_checkout',
                        'customer_account_create',
                        'customer_account_edit'
                    ]
        ]);

        $attribute->save();

    }

Attribute created successfully but when put email id in input type and save customer ,it not saving and display blank in field while others are working fine.

Please help what am I missing in code.

2
  • which error it show when you try to save this one? Have you check the magento log files? Commented Jan 6, 2020 at 14:47
  • @DhirenVasoya ,changing static to varchar working.
    – akgola
    Commented Jan 6, 2020 at 14:58

1 Answer 1

3

Change attribute type to varchar instead of static, and try again.

  $attributeCode = 'referred_email';

            $customerSetup->addAttribute(
                    \Magento\Customer\Model\Customer::ENTITY, $attributeCode, [
                'type' => 'varchar',
                'label' => 'Referral Email',
                'input' => 'text',
                'source' => '',
                'required' => false,
                'visible' => true,
                'position' => 201,
                'validate_rules' =>'{"input_validation":"email"}',
                'system' => false,
                'backend' => ''
                    ]
            );

            // show the attribute in the following forms
            $attribute = $customerSetup
                    ->getEavConfig()
                    ->getAttribute(
                            \Magento\Customer\Model\Customer::ENTITY, $attributeCode
                    )
                    ->addData(
                    ['used_in_forms' => [
                            'adminhtml_customer',
                            'adminhtml_checkout',
                            'customer_account_create',
                            'customer_account_edit'
                        ]
            ]);

            $attribute->save();

        }

I hope this will help you!!!

2
  • ,its working,when use static type.Please explain little.
    – akgola
    Commented Jan 6, 2020 at 14:57
  • static mean you have to create a new column on main entity_ table also. eg: customer_entity Commented Jan 7, 2020 at 14:33

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.