Skip to content

Magento 2 + Checkout Billing address Telephone Number Validation

I have created a plugin for billing address phone number validation, but it’s giving me an error

Exception #0 (Exception): Notice: Undefined index: dataScopePrefix in /var/www/html/test/vendor/magento/module-customer-custom-attributes/Block/Checkout/LayoutProcessor.php on line 139 

below is my code

CustomBillingAddressetcdi.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="MagentoCheckoutBlockCheckoutLayoutProcessor">
        <plugin name="validate_telephone_checkout_layoutprocessor" type="CustomBillingAddressPluginLayoutProcessor" sortOrder="100"/>
    </type>
</config>

plugin –

CustomBillingAddressPluginLayoutProcessor.php

<?php
declare(strict_types=1);

namespace CustomBillingAddressPlugin;

class LayoutProcessor
{
    /**
     * Telephone number validation
     *
     * @param MagentoCheckoutBlockCheckoutLayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
    public function afterProcess(
        MagentoCheckoutBlockCheckoutLayoutProcessor $subject,
        array $jsLayout
    ) {
        if (isset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
            ['payment']['children']['payments-list']['children']
        )) {
            foreach ($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                     ['payment']['children']['payments-list']['children'] as $key => $payment) {
                $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                ['payment']['children']['payments-list']['children'][$key]['children']['form-fields']['children']
                ['telephone']['validation'] = ['phoneLax' => true];
            }
        }
        return $jsLayout;
    }
}