The Advanced Validator module for Magento 2 allows you to implement custom validation rules for checkout fields. You can use regular expressions to validate fields like city, postcode, and more during the checkout process.
You can install the Advanced Validator module by manually adding the files to your Magento project.
- Download the module files.
- Unpack them into the following directory: app/code/M2S/AdvancedValidator/
After installation, enable the module by running the following commands:
bin/magento setup:upgrade
bin/magento module:enabled M2S_AdvancedValidator
bin/magento flush:cache
Once installed and enabled, the Advanced Validator module can be configured from the Magento Admin Panel.
- Navigate to the Configuration Page:
- Go to
Stores > Configuration > M2S > Advanced Validator.
- Go to
- Field ID:
enabled - Type: Select (Yes/No)
- Default Value: Yes
Description:
This option enables or disables the module. When set to Yes, the custom validation rules defined in the settings will be applied during checkout.
- Field ID:
validation_json_regex - Type: Grid of Columns (Country codes, Validation Key, Regex, Message)
Description:
This setting allows you to define custom validation rules for specific checkout fields using regular expressions. The columns you will see in the Admin Panel are:
| Country codes | Validation Key | Regex | Message |
|---|---|---|---|
PL |
custom-city-validation |
^[a-zA-Z0-9-_]+$ |
Please enter a valid city. |
PL,US |
custom-postcode-validation |
^\d{5,6}$ |
Please enter a valid postcode. |
- Country codes: Country codes of the field to validate.
- Validation Key: A unique identifier for the validation rule.
- Regex: Regular expression used for validation.
- Message: Custom error message shown when validation fails.
- Field ID:
fields_validation - Type: Grid of Columns (Field code, Validation Key, Validation Enabled, Form Type)
Description:
This setting maps specific fields to the validation rules defined above. You can assign validation rules to various fields in the checkout process, such as city or postcode. It also includes options to enable or disable validation for each field and select the form type it applies to (Shipping Address, Billing Address, or All Forms).
| Field code | Validation Key | Validation Enabled | Form Type |
|---|---|---|---|
city |
custom-city-validation |
Yes | All forms |
postcode |
custom-postcode-validation |
Yes | Shipping Address |
- Field code: The checkout field code where the validation rule should be applied.
- Validation Key: The validation key defined in the Custom Validation Regex List that should be used for the selected field.
- Validation Enabled: A toggle (Yes/No) to enable or disable validation for the field.
- Form Type: Defines the form the validation rule applies to:
All forms,Shipping Address,Billing Address.
- Field ID:
shipping_address_path - Type: Text
- Default Value: /shipping-step/children/shippingAddress/children/shipping-address-fieldset/children
Description:
This field allows you to define a custom path for the shipping address. If your checkout flow has a custom structure, specify the path where the shipping address is located.
- Field ID:
billing_address_path - Type: Text
- Default Value: /billing-step/children/payment/children/afterMethods/children/billing-address-form/children/form-fields/children
Description:
Similar to the shipping address path, this field allows you to specify a custom path for the billing address. This is useful if you have a custom address structure.
You need to have the option to display beyond payments enabled for this feature to work.
- In the Custom Validation Regex List, define a rule to accept only alphanumeric characters, hyphens, and underscores, ensuring that each Validation Key is unique. Optionally, you can also define Country Codes (e.g., PL, US) to restrict validation to specific countries.
| Country codes | Validation Key | Regex | Message |
|---|---|---|---|
PL |
custom-city-validation |
^[a-zA-Z0-9-_]+$ |
Please enter a valid city. |
- In the Fields Validation Rules, map the
cityfield to thecustom-city-validationkey:
| Field code | Validation Key | Validation Enabled | Form Type |
|---|---|---|---|
city |
custom-city-validation |
Yes | All forms |
The Sorting Configuration section allows you to configure the sort order of the fields on the checkout page.
In this scenario, you have the city field set to appear first, followed by the postcode field in the checkout forms:
- In the Sorting Configuration, define the sort order for the
cityandpostcodefields:
| Field code | Sort Order | Form Type |
|---|---|---|
city |
50 |
All forms |
postcode |
60 |
Shipping Address |
- The
cityfield will appear before thepostcodefield, ensuring a consistent and logical order based on your configuration.
This sorting configuration allows store admins to control the layout and order of fields in the checkout process, enhancing the user experience.
The Label Configuration section allows you to define custom labels for checkout fields.
In this scenario, you want to customize the label for the city field in the checkout form:
- In the Label Configuration, set the label for the
cityfield:
| Field code | Label | Form Type |
|---|---|---|
city |
Your City Name |
All forms |
This configuration allows store admins to control the text displayed for each field during the checkout process.
The Class Configuration section allows you to specify custom CSS classes for checkout fields.
In this scenario, you want to assign a CSS class to the city field:
- In the Class Configuration, set the CSS class for the
cityfield:
| Field code | CSS Class | Form Type |
|---|---|---|
city |
col-3 |
All forms |
This configuration allows store admins to control the appearance of fields by applying custom CSS classes.
- Developed by Piotr Wlosek
- piotr.wlosekx@gmail.com