cancel
Showing results for 
Search instead for 
Did you mean: 

Custom signup field not showing

Custom signup field not showing

I've been trying to add a custom filed to the customer registration page.

I can't get the field to show up. Am I missing something obvious here?

 

\view\frontend\layout

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block name="Sfs_SfCustomSignup" class="Magento\Framework\View\Element\Template" template="SfCustomSignup::sfExtraRegisterFields.phtml" />
</referenceContainer>
</body>
</page>

 

view\frontend\templates

 

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

<h1>testing</h1>

 

Setup\InstallSchema.php

 

<?php

namespace Sfslies\SfCustomSignup\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;

class InstallSchema implements InstallSchemaInterface
{
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

try {
$table = $setup->getConnection()->newTable(
$setup->getTable('sfslies_sf_signup_fields')
)->addColumn(
'id', Table::TYPE_INTEGER, null, ['identity' => true, 'nullable' => false, 'primary' => true], 'Table ID'
)->addColumn(
'company_name', Table::TYPE_TEXT, 255, ['nullable' => false]
)->addColumn(
'company_phone', Table::TYPE_TEXT, 15, ['nullable' => false]
)->addColumn(
'isCustomer', Table::TYPE_BOOLEAN, null, ['nullable' => false], 'Is existing customer'
)->setComment(
'Additional customer fields'
);

$setup->getConnection()->createTable($table);

$setup->endSetup();

} catch (\Zend_Db_Exception $e) {
echo "Caught exception: " . get_class($e) . "\n";
echo "Message: " . $e->getMessage() . "\n";
throwException($e);
}
}
}

 

Setup\InstallData

<?php

namespace Sfslies\SfCustomSignup\Setup;

use Magento\Customer\Model\ResourceModel\Attribute;
use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\Customer;

class InstallData implements InstallDataInterface
{
/** @var EavSetupFactory */
private $eavSetupFactory;
/** @var Config */
private $eavConfig;
/** @var Attribute */
private $attributeResource;


public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig, Attribute $attributeResource)
{
$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
$this->attributeResource = $attributeResource;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

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

try {

$eavSetup->addAttribute(Customer::ENTITY, 'customerName', [
'type' => 'varchar',
'label' => 'Customer Name',
'input' => 'text',
'required' => true,
'visible' => true,
'user_defined' => true,
'sort_order' => 990,
'position' => 990,
'system' => 0,
]);


$attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'customerName');

$attribute->setData('attribute_set_id', $attributeSetId);
$attribute->setData('attribute_group_id', $attributeGroupId);

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

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

} catch (LocalizedException $e) {
}
}
}

 

1 REPLY 1

Re: Custom signup field not showing

Hello @web_master12
Please refer the following link https://www.truecodex.com/course/magento-2/how-to-add-custom-field-in-magento-2-registration-form
I hope this will help you. If it helps you, please accept it as solution and give kudos.
Regards.