1

Hi dears I have going crazy to add a custom validation to vat field. I tried several ways, but I don't get an solve, I don't know why?!

I have a module Vendor/module

i have create a custom rule validation with this method Custom Validation For Checkout Field

custom rule who named : validation works for vat id in register page. When user wants to checkout available in source so However i add below line in etc > di.xml :

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Block\Widget\Name">
        <plugin name="registerformvalidate" type="Vendor\Module\Plugin\Customer\Block\Widget\Name"/>
    </type>
    <type name="Magento\Customer\Block\Widget\Taxvat">
        <plugin name="myvalidation" type="Vendor\Module\Plugin\Customer\Block\Widget\Taxvat"/>
    </type>
        <type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
        <plugin name="vat_id" type="Vendor\Module\Model\Checkout\LayoutProcessorPlugin" sortOrder="10"/>
    </type>
</config>

in LayoutProcessorPlugin i have below code:

namespace Vendor\Module\Model\Checkout;

class LayoutProcessorPlugin
{

public function afterProcess(
    \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
    array  $jsLayout
) {
    $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
            ['shippingAddress']['children']['shipping-address-fieldset']['children']['vat_id'] = [
        'component' => 'Magento_Ui/js/form/components/group',
        'label' => __('vat number'),
        'required' => true,
        'sortOrder' => 200,
        'type' => 'group',
        'children' => [
                [
                'component' => 'Magento_Ui/js/form/element/abstract',
                'config' => [
                    'customScope' => 'shippingAddress',
                    'template' => 'ui/form/field',
                    'elementTmpl' => 'ui/form/element/input'
                ],
                'dataScope' => 'vat_id',
                'provider' => 'checkoutProvider',
                'validation' => ["min_text_length" => 10 , "max_text_length" => 10,'myvalidation' => true],
            ],
        ]
    ];
    return $jsLayout;
    }
}

As you see on above min_length or max_length or other default of magento 2 validation rules works well.

What's my mistake for add custom validation for specific field ?

3
  • Did you add myvalidation to validator object? See link
    – GieDe
    Commented Sep 29, 2022 at 9:24
  • i tried that way doesn't works !
    – Peren
    Commented Sep 29, 2022 at 15:44
  • I think you should create a rule in the JS
    – Rana Zain
    Commented Oct 5, 2022 at 7:49

0

Your Answer

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