You can simply create your own element with modified validation process for this purpose. In your extension create a new file view/base/web/js/form/element/custom.js
with the following content:
define([
'Magento_Ui/js/form/element/abstract',
'Magento_Ui/js/lib/validation/validator'
], function (Abstract, validator) {
'use strict';
return Abstract.extend({
validate: function () {
var value = this.value(),
result = validator(this.validation, value, this.validationParams),
message = result.message,
isValid = result.passed;
this.error(message);
this.bubble('error', message);
if (!isValid) {
this.source.set('params.invalid', true);
alert('Please, specify a delivery date');
}
return {
valid: isValid,
target: this
};
}
});
});
Then, in LayoutProcessorPlugin change the 'component' value to the newly created element:
'component' => 'Modules_Deliverydate/js/form/element/custom'
In that way, you are able to extend the Abstract element and modify the "validate" method by removing field visibility checking etc. In this example the error message is being shown as an alert but you can use whatever you want.
Hope it will help.
Vendor/Module/registration.php & put below code.
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
add the module.xml file in Vendor/Module/etc/module.xml & put below code.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0"></module>
</config>
Create di.xml file in Vendor/Module/etc/di.xml & put below code.
<?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="Vendor\Module\Plugin\LayoutProcessor" sortOrder="100"/>
</type>
</config>
Now, create LayoutProcessor.php in Vendor\Module\Plugin\LayoutProcessor & put below code
<?php
namespace Vendor\Module\Plugin;
class LayoutProcessor
{
/**
* @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']['shipping-step']
['children']['shippingAddress']['children']['shipping-address-fieldset']['children']
)) {
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
['children']['shippingAddress']['children']['shipping-address-fieldset']['children']
['telephone']['validation'] = ['required-entry' => true, "validate-number"=>true, "min_text_length" =>10, "max_text_length" => 10 ];
}
/* config: checkout/options/display_billing_address_on = payment_method */
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) {
/* telephone */
$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
['payment']['children']['payments-list']['children'][$key]['children']['form-fields']['children']
['telephone']['validation'] = ['required-entry' => true,"validate-number"=>true, "min_text_length" =>10, "max_text_length" => 10 ];
}
}
return $jsLayout;
}
}
This will validate phone number on checkout
Best Answer
In your custom module directory, create a .js file implementing the validator. It should be located under /view/frontend/web/js/model directory.
Your custom validator must be added to the pool of “additional validators”. To do this, in the /view/frontend/web/js/view directory create a new .js file with the following content:
In your custom module directory, create a new /view/frontend/layout/checkout_index_index.xml file. In this file, add the following: