3

What is the purpose of validation.xml file in Magento 2? When and where do we use them ?

I see there are couple of validation.xml files in Magento 2 directory.

For example: vendor/magento/module-customer/etc/validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<validation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Validator/etc/validation.xsd">
    <entity name="customer">
        <rules>
            <rule name="check_eav">
                <entity_constraints>
                    <constraint alias="eav_data_validator" class="Magento\Eav\Model\Validator\Attribute\Data" />
                </entity_constraints>
            </rule>
            <rule name="metadata_form_data">
                <entity_constraints>
                    <constraint alias="metadata_data_validator" class="Magento\Customer\Model\Metadata\Validator" />
                </entity_constraints>
            </rule>
        </rules>
        <groups>
            <group name="save">
                <uses>
                    <use rule="check_eav"/>
                </uses>
            </group>
            <group name="form">
                <uses>
                    <use rule="metadata_form_data"/>
                </uses>
            </group>
        </groups>
    </entity>

    <entity name="customer_address">
        <rules>
            <rule name="check_eav">
                <entity_constraints>
                    <constraint alias="eav_data_validator" class="Magento\Eav\Model\Validator\Attribute\Data" />
                </entity_constraints>
            </rule>
        </rules>
        <groups>
            <group name="save">
                <uses>
                    <use rule="check_eav"/>
                </uses>
            </group>
        </groups>
    </entity>
</validation>
3

When order is created, Magento will go to Magento\Quote\Model\QuoteManagement->placeOrder() and there control goes to submit() and submitQuote() functions.

Then it checks if the customer is not guest (logged in) then it will validate the customer address.

If you dig more, you will see that it creates instances of validator classes by reading validation.xml from /magento/module-customer/etc/validation.xml and submitQuote() function calls Magento\Quote\Model\Customer\CustomerManagement->validateAddresses() which calls Magento\Framework\Validator\Factory->createValidator() function.

It is passed two arguments defined in Validation.xml

  1. Entity
  2. Group

Validator will find those entities and groups from Validation.xml and creates instances of classes for validation.

Hope this answers the question.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.