In Magento 2, Firstname and Lastname are consider as customer entities.
Customer’s Firstname and Lastname attributes have been added during Magento 2 installation.
Firstname and Lastname attributes are mostly used in the registration page and customer form.
Today, we are going to discuss, how to set minimum characters limit in Firstname and Lastname.
We are going to set minimum 2 characters limit in Firstname and Lastname.
For that, We need to change validate_rules column in customer_eav_attribute table for Firstname and Lastname attributes.
By default Firstname and Lastname attributes contain validate_rules value as {“max_text_length”:255,”min_text_length”:1}
We need to change that validate_rules column value to {“max_text_length”:255,”min_text_length”:2} for set minimum 2 characters.
Let’s see server side and client side validations:
1. Server side validation (After Form Submit)
You can create extension to get this functionality.
Extenson Name : Magemonkey_ValidateCustomerName
Create registration.php file in app/code/Magemonkey/ValidateCustomerName/ and add below code.
1 2 3 4 5 6 7 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magemonkey_ValidateCustomerName', __DIR__ ); |
Create module.xml in app/code/Magemonkey/ValidateCustomerName/etc/ and add below code.
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Magemonkey_ValidateCustomerName" setup_version="1.0.0"> <sequence> <module name="Magento_Customer" /> </sequence> </module> </config> |
Create UpgradeData.php file in app/code/Magemonkey/ValidateCustomerName/Setup/ and add below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<?php namespace Magemonkey\ValidateCustomerName\Setup; use Magento\Eav\Setup\EavSetup; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\UpgradeDataInterface; class UpgradeData implements UpgradeDataInterface { /** * @param EavSetup $eavSetupFactory */ public function __construct( \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory ) { $this->customerSetupFactory = $customerSetupFactory; } /** * Upgrades customer_eav_attribute table for validate_rules to set limit on character for first and lastname * * @param ModuleDataSetupInterface $setup * @param ModuleContextInterface $context * @return void */ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); /** @var CustomerSetup $customerSetup */ $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); $entityAttributes = [ 'customer_address' => [ 'firstname' => [ 'validate_rules' => '{"max_text_length":255,"min_text_length":2}' ], 'lastname' => [ 'validate_rules' => '{"max_text_length":255,"min_text_length":2}' ], ], 'customer' => [ 'firstname' => [ 'validate_rules' => '{"max_text_length":255,"min_text_length":2}' ], 'lastname' => [ 'validate_rules' => '{"max_text_length":255,"min_text_length":2}' ], ], ]; $this->upgradeAttributes($entityAttributes, $customerSetup); $setup->endSetup(); } protected function upgradeAttributes(array $entityAttributes, \Magento\Customer\Setup\CustomerSetup $customerSetup) { foreach ($entityAttributes as $entityType => $attributes) { foreach ($attributes as $attributeCode => $attributeData) { $attribute = $customerSetup->getEavConfig()->getAttribute($entityType, $attributeCode); foreach ($attributeData as $key => $value) { $attribute->setData($key, $value); } $attribute->save(); } } } } |
Above script will change validate_rules in customer_eav_attribute table to apply validation for server side.
2. Client Side Validation (Before Form Submit)
Override /vendor/magento/module-customer/view/frontend/templates/widget/name.phtml file to add client side validations.
Add minimum-length-2 validate-length class in Firstname and Lastname input elements.
So Firstname and Lastname inputes look like below in name.phtml file,
Firstname input :
1 2 3 4 5 |
<input type="text" id="<?= $block->escapeHtmlAttr($block->getFieldId('firstname')) ?>" name="<?= $block->escapeHtmlAttr($block->getFieldName('firstname')) ?>" value="<?= $block->escapeHtmlAttr($block->getObject()->getFirstname()) ?>" title="<?= $block->escapeHtmlAttr($block->getStoreLabel('firstname')) ?>" class="minimum-length-2 validate-length input-text <?= $block->escapeHtmlAttr($block->getAttributeValidationClass('firstname')) ?>" <?php if ($block->getAttributeValidationClass('firstname') == 'required-entry') echo ' data-validate="{required:true}"' ?>> |
Lastname input :
1 2 3 4 5 |
<input type="text" id="<?= $block->escapeHtmlAttr($block->getFieldId('lastname')) ?>" name="<?= $block->escapeHtmlAttr($block->getFieldName('lastname')) ?>" value="<?= $block->escapeHtmlAttr($block->getObject()->getLastname()) ?>" title="<?= $block->escapeHtmlAttr($block->getStoreLabel('lastname')) ?>" class="minimum-length-2 validate-length input-text <?= $block->escapeHtmlAttr($block->getAttributeValidationClass('lastname')) ?>" <?php if ($block->getAttributeValidationClass('lastname') == 'required-entry') echo ' data-validate="{required:true}"' ?>> |
[crayon-63e0a3b0678e9607691834/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.