Try our conversational search powered by Generative AI!

Quan Mai
Sep 19, 2014
  2959
(3 votes)

Customize the limitation of catalog content’s code.

This is my first blog post, so comments and suggestions are welcome!

Recently we got a “feature request” from a customer, saying that the code should be able to contain commas, and they further asked to be able to customize the limitation of catalog content’s code, based on a Regex of their choice.

We already support that!

You might already know how it works, but if you don’t, then it’s quite simple:

In CatalogContentBase, we have a protected constant string, defined like this:

protected const string ValidCodeCharactersRegex = @"^[A-Za-z0-9\-_()\.\s]*$";

And in EntryContentBase and NodeContent, Code property is defined by something like this (other attributes are omitted):

[RegularExpression(ValidCodeCharactersRegex, ErrorMessage = "/commerce/validation/codecharactersonly")]

public virtual string Code { get; set; }

It’s quite clear to understand the intention here: We’ll validate the inputted string for Code against ValidCodeCharactersRegex, and if it’s not validated, then we show this message:

clip_image002[2]

But it’s easily to change the behavior

In your catalog content model class, you just need to override the ValidCodeCharactersRegex field with new one, for example, to allow the commas:

protected new const string ValidCodeCharactersRegex = @"^[A-Za-z0-9\,\-_()\.\s]*$";

Then build project and you’ll be good to go:

clip_image004[2]

To avoid code duplication, simply define wrapper classes for catalog content model, and inherit from them instead. For example:

[CatalogContentType(GUID = "BE40A3E0-49BC-48DD-9C1D-819C2661C9BC")]

public class VariantItemContent : VariationContent

{

protected new const string ValidCodeCharactersRegex = @"^[A-Za-z0-9\,\-_()\.\s]*$";

}

Then all of class inherited from VariantItemContent will use new validation instead of the default one.

In CatalogContentBase, we have another field for the validation of SeoUri, named ValidUrlCharactersRegex, which you can also change to fit your need.

Happy customizing!

Sep 19, 2014

Comments

Arve Systad
Arve Systad Sep 23, 2014 02:16 PM

*Just* setting the ValidCodeCharactersRegex did not work for me. Did you get this to work by *just* setting a new ValidCodeCharactersRegex?

The following, however, worked fine:

protected new const string ValidCodeCharactersRegex = @"^[A-Za-z0-9\,\-_()\.\s]*$";
[RegularExpression(ValidCodeCharactersRegex, ErrorMessage = "/commerce/validation/codecharactersonly")]
public override string Code { get; set; }

Quan Mai
Quan Mai Sep 24, 2014 10:55 AM

You are correct. I might have tested with EntryContentBase where Code is defined. Thanks for pointing out.

Please login to comment.
Latest blogs
Why C# Developers Should Embrace Node.js

Explore why C# developers should embrace Node.js especially with Optimizely's SaaS CMS on the horizon. Understand the shift towards agile web...

Andy Blyth | May 2, 2024 | Syndicated blog

Is Optimizely CMS PaaS the Preferred Choice?

As always, it depends. With it's comprehensive and proven support for complex business needs across various deployment scenarios, it fits very well...

Andy Blyth | May 2, 2024 | Syndicated blog

Adding market segment for Customized Commerce 14

Since v.14 of commerce, the old solution  for adding market segment to the url is not working anymore due to techinal changes of .NET Core. There i...

Oskar Zetterberg | May 2, 2024

Blazor components in Optimizely CMS admin/edit interface

Lab: Integrating Blazor Components into Various Aspects of Optimizely CMS admin/edit interface

Ove Lartelius | May 2, 2024 | Syndicated blog