Quan Mai
Sep 19, 2014
  3015
(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
Introducing Optimizely Graph Source .NET SDK

Overview Of Optimizely Graph Optimizely Graph is a cutting-edge, headless content management solution designed to integrate seamlessly with any...

Jake Minard | Oct 10, 2024

Content Search with Optimizely Graph

Optimizely Graph lets you fetch content and sync data from other Optimizely products. For content search, this lets you create custom search tools...

Dileep D | Oct 9, 2024 | Syndicated blog

Omnichannel Analytics Simplified – Optimizely Acquires Netspring

Recently, the news broke that Optimizely acquired Netspring, a warehouse-native analytics platform. I’ll admit, I hadn’t heard of Netspring before,...

Alex Harris - Perficient | Oct 9, 2024 | Syndicated blog

Problem with language file localization after upgrading to Optimizely CMS 12

Avoid common problems with xml file localization when upgrading from Optimizely CMS 11 to CMS 12.

Tomas Hensrud Gulla | Oct 9, 2024 | Syndicated blog