1

How to remove the last name field in registration form?

1
  • 2
    did you get the solution to remove the validations for last name? Commented Dec 27, 2016 at 11:53

4 Answers 4

3

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.

8
  • 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?
    – TFS
    Commented 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
  • @S H Patel, it is not working.
    – TFS
    Commented Jul 29, 2016 at 10:51
  • What error come now ? Commented Jul 29, 2016 at 10:53
  • 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
2

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

0

try this :

UPDATE eav_attribute SET is_required = 0 WHERE attribute_code = 'lastname' and entity_type_id = 2
0

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;
}
2
  • 1
    Why do you recommend to hide a field with css instead of using other proper methods?
    – amitshree
    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.
    – cjohansson
    Commented May 18, 2018 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.