Basically I am trying to add custom validation rule in existing street field of shipping address form Checkout in magento 2 where pobox should not be allowed. I tried using mixin with requirejs-config.js which works well as it gets called till (console.log(hello)) but I am not sure why my 'validate-street-part' is not getting called from layoutprocessor and adding 'validate-street-part' to (mage/validation.js). Any advise or suggestion would be appreciated.
\app\design\frontend\Vendor\Magento_Checkout\web\js\pobox-mixin.js
define([
'jquery',
'jquery/ui',
'jquery/validate',
'mage/translate'
], function ($) {
"use strict";
return function () {
console.log("hello");
$.validator.addMethod(
'validate-street-part',
function (v) {
var pattern = /^([post]*)$/.test(v);
console.log(pattern);
if(!pattern){
console.log("return true");
return true;
}else{
console.log("return false");
return false;
}
},
$.mage.__("Please note that we cannot ship to PO Boxes. Please enter a physical address.")
);
};
});
app\design\frontend\Vendor\Magento_Checkout\requirejs-config.js
var config = {
config: {
mixins: {
'mage/validation': {
'Magento_Checkout/js/pobox-mixin' : true
}
}
}
};
app\code\Vendor\Checkout\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="rewrite-street" type="Vendor\Checkout\Block\Checkout\LayoutProcessor" sortOrder="10"/>
</type>
</config>
app\code\Vendor\Checkout\Block\Checkout\LayoutProcessor.php
<?php
namespace Vendor\Checkout\Block\Checkout;
class LayoutProcessor
{
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']['street']['validation']['validate-street-part'] = 1;
return $jsLayout;
}
}