Hi dears I want to add validation for vat number in shipping or billing addresses when user wants to checkout.
As old I have a validation module who has works well for fields in register page.
Now I want to add my custom validation for tax/vat number (vat_id)
Note: I enabled show vat number on storefront.
I Created a file in setup > UpgradeData.php with Below Code:
namespace VendorModuleSetup;
use MagentoEavSetupEavSetup;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupUpgradeDataInterface;
class UpgradeData implements UpgradeDataInterface
{
public function __construct(
MagentoCustomerSetupCustomerSetupFactory $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":213,"min_text_length":2}'
],
'lastname' => [
'validate_rules' => '{"max_text_length":255,"min_text_length":2}'
],
'vat_id' => [
'validate_rules' => '{"max_text_length":9,"min_text_length":9,"input_validation":"myvalidation"}',
'attribute_model' => 'MagentoCustomerModelAttribute',
'is_required' => true,
'frontend_label' => 'Vat Code',
'frontend_input' => 'text',
'used_in_forms' => ['adminhtml_customer','adminhtml_checkout','adminhtml_customer_address','customer_address_edit','customer_account_create','customer_account_edit','customer_register_address'],
],
],
'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, MagentoCustomerSetupCustomerSetup $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();
}
}
}
}
I used above way but when I go for checkout show below error:
Notice: Undefined index: myvalidation in /public_html/vendor/magento/module-checkout/Block/Checkout/AttributeMerger.php on line 174