Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Quan Mai
Sep 19, 2014
  2951
(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
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog