0

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

Custom\BillingAddress\etc\di.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="Magento\Checkout\Block\Checkout\LayoutProcessor">
        <plugin name="validate_telephone_checkout_layoutprocessor" type="Custom\BillingAddress\Plugin\LayoutProcessor" sortOrder="100"/>
    </type>
</config>

plugin -

Custom\BillingAddress\Plugin\LayoutProcessor.php

<?php
declare(strict_types=1);

namespace Custom\BillingAddress\Plugin;

class LayoutProcessor
{
    /**
     * Telephone number validation
     *
     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $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;
    }
}
0

You added wrong validation type. Please replace validation type "phoneLax" to "validate-phoneLax" as shown in following code:

<?php
declare(strict_types=1);

namespace Custom\BillingAddress\Plugin;

class LayoutProcessor
{
    /**
     * Telephone number validation
     *
     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $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'] = ['validate-phoneLax' => true];    // Change validation type 'phoneLax' to 'validate-phoneLax'
            }
        }
        return $jsLayout;
    }
}
1
  • not working !!! – Yogesh Feb 23 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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