November Happy Hour will be moved to Thursday December 5th.

Quan Mai
Sep 19, 2014
  3032
(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
Adding Geolocation Personalisation to Optimizely CMS with Cloudflare

Enhance your Optimizely CMS personalisation by integrating Cloudflare's geolocation headers. Learn how my Cloudflare Geo-location Criteria package...

Andy Blyth | Nov 26, 2024 | Syndicated blog

Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog