How to remove the last name field in registration form?
4 Answers
You have to copy file from
projectroot\vendor\magento\module-customer\view\frontend\templates\widget\name.phtml
And paste to in you theme.
You can find class field-name-lastname in code. delete complete this div for last name.
You have to run below query in your database for set lastname as optional for customer.
UPDATE eav_attribute SET is_required = 0 WHERE attribute_code = 'lastname' and `entity_type_id` = 1
Run Command : php bin/magento cache:clean
Let me know if you have any issue.
-
It is not a complete solution. It just hide the field from display. But the form validation is not passed as last name is a required form. How to remove the validation also?– TFSCommented Jul 29, 2016 at 9:36
-
You can see my updated answer for set last name as optional. Commented Jul 29, 2016 at 10:39
-
-
-
1@SHPatel I have fallowed above 2 steps, but still getting the validation error as lastname is a required field. Could you help me. Commented Dec 27, 2016 at 12:01
You need to run SQL query in your database to set last name as optional or remove:
UPDATE eav_attribute SET is_required = 0 WHERE attribute_code = 'lastname' and entity_type_id = 1
Go to below file where you can find class field-name-lastname in code. delete complete this div for last name.
vendor\magento\module-customer\view\frontend\templates\widget\name.phtml
Note : We recommended to override the core files
try this :
UPDATE eav_attribute SET is_required = 0 WHERE attribute_code = 'lastname' and entity_type_id = 2
If you are in the context of class Setup/InstallData
and method install()
while implementing \Magento\Framework\Setup\InstallDataInterface
you can do something like this:
/**
* @param \Magento\Framework\Setup\ModuleDataSetupInterface $setup
* @param \Magento\Framework\Setup\ModuleContextInterface $context
*/
public function install(
\Magento\Framework\Setup\ModuleDataSetupInterface $setup,
\Magento\Framework\Setup\ModuleContextInterface $context)
{
$setup->startSetup();
$customerSetup = $this->_customerSetupFactory->create(['setup' => $setup]);
/** @var \Magento\Customer\Setup\CustomerSetup $customerSetup */
// Make lastname optional
$customerSetup->updateAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'lastname',
'is_required',
false
);
}
Then you can hide the lastname
field with CSS like this:
fieldset.fieldset.create.info .field-name-lastname
{
display: none;
}
-
1Why do you recommend to hide a field with css instead of using other proper methods? Commented May 18, 2018 at 6:59
-
Sometimes a client is not willing to pay for the extra work to make a more proper solution, a CSS fix is done in a minute or two while a more proper solution could take hours depending on what theme hierarchy and modules you have on a setup. Commented May 18, 2018 at 8:11