cancel
Showing results for 
Search instead for 
Did you mean: 

Form fields not transferring data to the backend

Highlighted

Form fields not transferring data to the backend

I created a few new fields for the customer signup form. The ones that were added through the InstallData script work just fine.

 

The one's that I added with a patch are created in the database, but then don't get sent to the backend. The fields also show up in the backend form, but no data from those fields is saved.

 

Input of field that works:

```

<div id="customer_code" class="field customer_code">
<label for="customer_code" class="label"><span><?php /* @escapeNotVerified */
echo __('Customer Code') ?></span></label>
<div class="control">
<input type="text" name="customer_code" id="customer_code" title="<?php /* @escapeNotVerified */
echo __('customer_code') ?>" class="input-text">
</div>
</div>

 ```

Input of field that doesn't work

<div id="interested_in" class="field interested_in">
  <label for="interested_in" class="label"><span><?php /* @escapeNotVerified */ echo __('Interested In') ?></span></label>
    <div class="control">
     <input type="text" name="interested_in" id="interested_in" title="<?php /* @escapeNotVerified */
       echo __('interested_in') ?>" class="input-text">
      </div>
</div>

app/code/companyname/modulename/Setup/Patch/Data AddCustomerAttributes.php

 

```

<?php
namespace <company>\<modulename>\Setup\Patch\Data;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

/**
* Class AddCustomerAttribute
* @package Vendor\Module\Setup\Patch\Data
*/
class AddCustomerAttributes implements DataPatchInterface
{
/**
* @var ModuleDataSetupInterface
*/
protected $moduleDataSetup;

/**
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;

/**
* @var AttributeSetFactory
*/
protected $attributeSetFactory;

/**
* AddCustomerPhoneNumberAttribute constructor.
* @param ModuleDataSetupInterface $moduleDataSetup
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}

/**
* {@inheritdoc}
*/
public function apply()
{
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);

$customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
$attributeSetId = $customerEntity->getDefaultAttributeSetId();

$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

$customerSetup->addAttribute(
Customer::ENTITY,
'existing_customer',
[
'type' => 'int',
'label' => 'Existing Customer',
'input' => 'boolean',
'required' => false,
'sort_order' => 160,
'position' => 160,
'visible' => true,
'user_defined' => true,
'unique' => false,
'system' => false,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_html_allowed_on_front' => true,
'visible_on_front' => true
]
);

$attribute = $customerSetup->getEavConfig()->getAttribute(
Customer::ENTITY,
'existing_customer'
);

$attribute->addData(
[
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],
]
);


$customerSetup->addAttribute(
Customer::ENTITY,
'interested_in',
[
'type' => 'varchar',
'label' => 'Interested In',
'input' => 'text',
'required' => false,
'sort_order' => 161,
'position' => 161,
'visible' => true,
'user_defined' => true,
'unique' => false,
'system' => false,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_html_allowed_on_front' => true,
'visible_on_front' => true
]
);

$attribute = $customerSetup->getEavConfig()->getAttribute(
Customer::ENTITY,
'interested_in'
);

$attribute->addData(
[
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],
]
);


}

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

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

Re: Form fields not transferring data to the backend

Hi @web_master12 ,

 

As you can see in the code we set that attribute should be used in forms:
adminhtml_customer

If we want to use it at the Customer Edit and Customer Registration Page then you need to add customer_account_edit and customer_account_create as well.
It should be like this:

'used_in_forms' =>

[‘adminhtml_customer’, ‘customer_account_edit’, ‘customer_account_create’] 

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!

Highlighted

Re: Form fields not transferring data to the backend

Thanks, but that's not the issue at all. Even when I create a user in the backend, the fields are there, but no data entered into those fields is saved

Highlighted

Re: Form fields not transferring data to the backend

Hi @web_master12,

 

Can you try changing one property of attribute like this

System  => 0 

Instead of System => false and try to compare other properties as well from below link

https://github.com/magento/magento2/issues/1393

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!