Important

You are browsing the documentation for version 3.1 of OroCommerce, OroCRM and OroPlatform, which is no longer maintained. Read version 5.1 (the latest LTS version) of the Oro documentation to get up-to-date information.

See our Release Process documentation for more information on the currently supported and upcoming releases.

Customize Products SKU Validation

The ‘/^[a-zA-Z0-9]*$/’ pattern is used for Product SKU validation by default. This pattern provides the possibility to save alphanumeric symbols with additional dash and underscore symbols. If you need to extend the default pattern for Product SKU Validation or make it stricter, override the oro_product.sku.regex_pattern parameter and the translation for the validation message.

Override Parameter oro_product.sku.regex_pattern

There are 2 ways to override the oro_product.sku.regex_pattern parameter in your own bundle:

  1. Add the oro_product.sku.regex_pattern parameter to the Resources/config/services.yml file in your bundle.

    src/Acme/Bundle/DemoBundle/Resources/config/services.yml
    1 parameters:
    2     ...
    3     oro_product.sku.regex_pattern: '/^[a-z]*$/'
    
  2. Write appropriate CompilerPass in your bundle

    src/Acme/DemoBundle/DependencyInjection/Compiler/OverrideProductSKUCompilerPass.php
     1 namespace Acme\DemoBundle\DependencyInjection\Compiler;
     2
     3 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
     4 use Symfony\Component\DependencyInjection\ContainerBuilder;
     5
     6 class OverrideProductSKUCompilerPass implements CompilerPassInterface
     7 {
     8     public function process(ContainerBuilder $container)
     9     {
    10         $container->setParameter('oro_product.sku.regex_pattern', '/^[a-z]*$/');
    11     }
    12 }
    

    and register this CompilerPass in the build() method of the bundle class

    src/Acme/DemoBundle/DemoBundle.php
     1 namespace DemoBundle;
     2
     3 use Acme\DemoBundle\DependencyInjection\Compiler\OverrideProductSKUCompilerPass;
     4 use Symfony\Component\DependencyInjection\ContainerBuilder;
     5 use Symfony\Component\HttpKernel\Bundle\Bundle;
     6
     7 class DemoBundle extends Bundle
     8 {
     9     public function build(ContainerBuilder $container)
    10     {
    11         parent::build($container);
    12
    13         $container->addCompilerPass(new OverrideProductSKUCompilerPass());
    14     }
    15 }
    

Override the Validation Message

If you need to change the default ‘This value should contain only latin letters, numbers and symbols “-” or “_”.’ validation message, override the oro.product.sku.not_match_regex translation key. To do that, add the appropriate translation to the translations/validators.en.yml file in your bundle:

translations/validators.en.yaml
1 oro.product.sku.not_match_regex: This vaule should contain only latin letters in lower case.