Add custom validations before order placement
What's in this topic
This topic describes how to add custom validations to be performed before the order is placed during checkout. Namely, the validations which are performed after a shopper clicks the Place Order button. Writing the validation logic itself is not covered in this topic.
Overview
To add custom validations before the order placement action, you must do the following:
- Create the validator.
- Add validator to the validators pool.
- Declare the validation in the checkout layout.
Create the validator
For the sake of compatibility, upgradability and easy maintenance, do not edit the default Magento code, add your customizations in a separate module. For your checkout customization to be applied correctly, your custom module should depend on the Magento_Checkout
module. Do not use Ui
for your custom module name, because %Vendor%_Ui
notation, required when specifying paths, might cause issues.
In your custom module directory, create a .js
file implementing the validator. It should be located under <your_module_dir>/view/frontend/web/js/model
directory.
Following is a sample of the validator .js
file. It must necessarily implement the validate()
method:
Add validator to the validators pool
Your custom validator must be added to the pool of “additional validators”. To do this, in the <your_module_dir>/view/frontend/web/js/view
directory create a new <your-validation>.js
file with the following content:
Declare the validation in the checkout layout
In your custom module directory, create a new <your_module_dir>/view/frontend/layout/checkout_index_index.xml
file.
In this file, add the following: