Quan Mai
Sep 19, 2014
  2981
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024