0

I want to override vendor/magento/module-customer/etc/validation.xml This validation.xml in custom module.

Baically I want to change below file location with my custom module file

<rule name="check_name">
   <entity_constraints>
        <constraint alias="name_validator" class="Magento\Customer\Model\Validator\Name" />
   </entity_constraints>
</rule>

This Magento\Customer\Model\Validator\Name with Vendor\Module\Model\Validator\Name

If anyone have any idea please share.

TIA

1
  • You could try a rewrite or plugin. Also, have you tried creating a custom extension and adding in etc/validation.xml the code above? Commented Jul 5, 2022 at 17:18

1 Answer 1

1

You can override the whole class or use plugin to override the function only. I would prefer plugin.

  1. To override the class, in your etc/di.xml add this:

    <preference for="Magento\Customer\Model\Validator\Name" type="Vendor\Module\Model\Validator\Name" />

Now your Vendor\Module\Model\Validator\Name class:

<?php 
namespace Vendor\Module\Model\Validator;
class Name extends \Magento\Customer\Model\Validator\Name
{
  //your code
}

More info: https://inchoo.net/magento-2/overriding-classes-magento-2/

  1. For plugin method check this: https://developer.adobe.com/commerce/php/development/components/plugins/
2
  • Plugin does not override the private function, And I want to override the private function. But can't achieve it with preference. So I want to override validation.xml to change the path Commented Jul 6, 2022 at 4:05
  • <preference> is for overriding the whole class. If you are overriding the private function then you will need to override the public function that calls the private function. :) Commented Jul 6, 2022 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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