3

So i'm creating a custom attribute for Customer, the attribute was created with success. But when i try to register a Customer, it says that "field is required" even though it's filled. So i turned the field to not required, but when i tried to register Customer, the field don't save the values inputed.

When i insert the field value with admin menu it works. But it doesn't works on customer register.

I followed this tutorial: https://store.magenest.com/blog/add-custom-field-to-registration-page-magento-2/

Attribute/Cpf/Setup/Patch/Data/AddCpfAttribute.php

    namespace Attribute\Cpf\Setup\Patch\Data;

use Magento\Catalog\Ui\DataProvider\Product\ProductCollectionFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
use Psr\Log\LoggerInterface;

/**
* Class AddCpfAttribute
* @package Attribute\Cpf\Setup\Patch\Data
*/
class AddCpfAttribute implements DataPatchInterface, PatchRevertableInterface
{
   /**
    * @var ModuleDataSetupInterface
    */
   private $moduleDataSetup;
   /**
    * @var EavSetupFactory
    */
   private $eavSetupFactory;
   /**
    * @var ProductCollectionFactory
    */
   private $productCollectionFactory;
   /**
    * @var LoggerInterface
    */
   private $logger;
   /**
    * @var Config
    */
   private $eavConfig;
   /**
    * @var \Magento\Customer\Model\ResourceModel\Attribute
    */
   private $attributeResource;

   /**
    * AddCpfAttribute constructor.
    * @param EavSetupFactory $eavSetupFactory
    * @param Config $eavConfig
    * @param LoggerInterface $logger
    * @param \Magento\Customer\Model\ResourceModel\Attribute $attributeResource
    */
   public function __construct(
       EavSetupFactory $eavSetupFactory,
       Config $eavConfig,
       LoggerInterface $logger,
       \Magento\Customer\Model\ResourceModel\Attribute $attributeResource,
       \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
   ) {
       $this->eavSetupFactory = $eavSetupFactory;
       $this->eavConfig = $eavConfig;
       $this->logger = $logger;
       $this->attributeResource = $attributeResource;
       $this->moduleDataSetup = $moduleDataSetup;
   }

   /**
    * {@inheritdoc}
    */
   public function apply()
   {
       $this->moduleDataSetup->getConnection()->startSetup();
       $this->addCpfAttribute();
       $this->moduleDataSetup->getConnection()->endSetup();
   }

   /**
    * @throws \Magento\Framework\Exception\AlreadyExistsException
    * @throws \Magento\Framework\Exception\LocalizedException
    * @throws \Zend_Validate_Exception
    */
   public function addCpfAttribute()
   {
       $eavSetup = $this->eavSetupFactory->create();


       $eavSetup->addAttribute(
           \Magento\Customer\Model\Customer::ENTITY,
           'cpf',
           [
               'type' => 'varchar',
               'label' => 'CPF',
               'input' => 'text',
               'required' => 1,
               'visible' => 1,
               'user_defined' => 1,
               'sort_order' => 999,
               'position' => 999,
               'system' => 0
           ]
       );

       $attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
       $attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);

       $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'cpf');
       $attribute->setData('attribute_set_id', $attributeSetId);
       $attribute->setData('attribute_group_id', $attributeGroupId);

       $attribute->setData('used_in_forms', [
           'adminhtml_customer',
       ]);

       $this->attributeResource->save($attribute);
   }

   /**
    * {@inheritdoc}
    */
   public static function getDependencies()
   {
       return [];
   }

   /**
    *
    */
   public function revert()
   {
   }

   /**
    * {@inheritdoc}
    */
   public function getAliases()
   {
       return [];
   }
}

Attribute/Cpf/view/frontend/layout/customer_account_create.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="form.additional.info">
           <block class="Magento\Framework\View\Element\Template"
                  name="cpf"
                  template="Attribute_Cpf::extra_field.phtml"/>
       </referenceContainer>
    </body>
</page>

Attribute/Cpf/view/frontend/templates/extra_field.phtml

<div class="field cpf required">
   <label class="label" for="cpf">
       <span><?= $block->escapeHtml(__('CPF')) ?></span>
   </label>
   <div class="control">
       <input type="text" name="cpf" id="cpf" value="" title="<?= $block->escapeHtmlAttr(__('CPF')) ?>" class="input-text" data-validate="{required:true}">
   </div>
</div>
1
0

So i solved this following the link on the comments, Magento2 : user define customer attribute not save value while create / save from admin and i noticed that this was missing in the Attribute/Cpf/Setup/Patch/Data/AddCpfAttribute.php:

$attribute->setData('used_in_forms', [
   'adminhtml_customer',
   'customer_account_create',
   'customer_account_edit'
]);

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.