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
    2
    3
     parameters:
         ...
         oro_product.sku.regex_pattern: '/^[a-z]*$/'
    
  2. Write appropriate CompilerPass in your bundle

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

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

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

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.