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.

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

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!!!

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

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.