Try our conversational search powered by Generative AI!

Mattias Bomelin
May 16, 2016
  1908
(2 votes)

Business Foundation Validators

First off, what is Business Foundation (BF)?

BF is basically a meta system where you can define an entity type and its properties. But it doesn’t stop there, the main differences between BF and the “regular” meta system in Commerce are that you can define relations between your meta classes, and that there’s a management UI you can extend and modify without changing any code. Sounds easy, but unfortunately not very well documented.

The management UI basically consists of views for displaying the data and forms for editing the data. Whenever an entity is saved, using the UI or using the api’s, the data is validated using a set of configurable properties and validators. So why implement you custom validation when it's right there in front of you, and used both by CM and all your application code.

Every meta class is represented in the mcmd_MetaClass table where the class level data is stored.

The field XSValidators contains XML data defining how the fields in a meta class should be validated. For each field you may define the validator implementation to use along with some validator specific properties.

Below is a partial sample from the Contact meta class.

<?xml version="1.0"?>

<ArrayOfValidator xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Validator TypeName="Mediachase.BusinessFoundation.Data.GuidFieldValidator, Mediachase.BusinessFoundation.Data">
    <Params FieldName="ContactId" AllowNull="False" />
  </Validator>
  <Validator TypeName="Mediachase.BusinessFoundation.Data.DateTimeFieldValidator, Mediachase.BusinessFoundation.Data">
    <Params FieldName="Created" AllowNull="False" />
  </Validator>
  <Validator TypeName="Mediachase.BusinessFoundation.Data.StringFieldValidator, Mediachase.BusinessFoundation.Data">
    <Params FieldName="FullName" AllowNull="False" MaxLength="200" />
  </Validator>
  <Validator TypeName="Mediachase.BusinessFoundation.Data.EnumFieldValidator, Mediachase.BusinessFoundation.Data">
    <Params FieldName="CustomerGroup" AllowNull="True" EnumTypeName="ContactGroup" />
  </Validator>
  <Validator TypeName="Mediachase.BusinessFoundation.Data.ReferenceFieldValidator, Mediachase.BusinessFoundation.Data">
    <Params FieldName="PreferredShippingAddressId" AllowNull="True" />
  </Validator>
  <Validator TypeName="Mediachase.BusinessFoundation.Data.BooleanFieldValidator, Mediachase.BusinessFoundation.Data">
    <Params FieldName="MyCustomBool" AllowNull="True" />
  </Validator>
</ArrayOfValidator>

 

As you see there are a variety of validator types, and not all properties can be managed in the Business Foundation management UI. Also, when setting up a large e-commerce project which might need deployments to several different environments we'd like to automate the setup of these validators but that's another story.

A validator is defined using ntype names and assemblies making it possible to create custom validators implementing IFieldValidator and IValidator. Yay :)

But first, let's take a look at the existing validators.

AllowNull is a parameter used in most validators and defines whether or not to accept a null value.

StringFieldValidator

This validator is used to validate a string input.

Maxlength defines the maximum char length that may be input. Note that this is just a validator, make sure not to allow a string longer than you can actually store.

RegexPattern is a nifty property where you can define a regular expression to validate the input against.

For example, if you create a string field in the BF management UI and select the format "e-mail" the RegexPattern would be set to \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* which is in my opinion far too basic. Just pop in a new regex and you're all set :)

Other use cases could be to validate phone numbers, postal codes, allowed chars for name fields and so on. Also, there is no MinValue property so you could easily use the regex property for validating your minimum string length.

GuidFieldValidator

This validator is used to validate if the input is a valid guid. If the value can be parsed to a guid it's valid.
 

BooleanFieldValidator

Validates if the value is of boolean type.
 

DateTimeFieldValidator

Validates if the value is of DateTime type, and checks the below properties if present.

MinValue - can be used to set the lowest allowed value

MaxValue - can be used to set the highest allowed value

For birth dates MinValue and MaxValue could be used to limit how young or old contacts that may be created, or if we would store for example a member date we wouldn't want to have dates from before the member system was created. If you would like to disallow dates in the future you could create your own validator and add a property for enabling/disabling future date validation.

CurrencyFieldValidator

Validates if the value is of decimal type. Not sure why it's been named CurrencyFieldValidator since it's just a basic decimal validator. 

DecimalFieldValidator

Works the same as the CurrencyFieldValidator but also has MinValue and MaxValue properties to limit the minimum and maximum value that may be entered. 

DoubleFieldValidator

Works the same as for decimal but validates if the value type is double. Also accepts MinValue and MaxValue. 

IntegerFieldValidator

Works the same as for decimal but validates if the value type is integer. Also accepts MinValue and MaxValue. 

EnumFieldValidator

Validates if the value maps to a value in an enum meta field.

EnumTypeName is used to define which meta field to compare the input value with.

IsMultiValue defines whether or not multiple values may be input. If not accepting multiple values correct input type would be int or string, and for multiple values input types would be int[] and string[] 

FileFieldValidator

Validates if the value is of type FileInfo.

ValidationExpression property allows you to specify a regex to validate the file name againts.

IdentifierFieldValidator

This validator is a bit weird, it validates if the value is of type MetaIdentifierValue or of type string.

If the type is string any value is valid which makes me confused of the use case.
 

ReferenceFieldValidator

Validates if the value is a PrimaryKeyId

 

May 16, 2016

Comments

Please login to comment.
Latest blogs
Configured Commerce - Infrastructure Updates Ahoy!

I'm very happy to share an important milestone - we no longer have any customers in our legacy v1 environment!  This means that the Configured...

John McCarroll | May 10, 2024

A day in the life of an Optimizely Developer - Enabling Opti ID within your application

Hello and welcome to another instalment of A Day In The Life Of An Optimizely developer, in this blog post I will provide details on Optimizely's...

Graham Carr | May 9, 2024

How to add a custom property in Optimizely Graph

In the Optimizely CMS content can be synchronized to the Optimizely Graph service for it then to be exposed by the GraphQL API. In some cases, you...

Ynze | May 9, 2024 | Syndicated blog

New Security Improvement released for Optimizely CMS 11

A new security improvement has been released for Optimizely CMS 11. You should update now!

Tomas Hensrud Gulla | May 7, 2024 | Syndicated blog

Azure AI Language – Key Phrase Extraction in Optimizely CMS

In this article, I demonstrate how the key phrase extraction feature, offered by the Azure AI Language service, can be used to generate a list of k...

Anil Patel | May 7, 2024 | Syndicated blog

Webinar: Get Started with AI within Optimizely CMS

Join us for the webinar "Get Started with AI in Optimizely CMS" on Wednesday, May 8th. Don't forget to register!

Luc Gosso (MVP) | May 7, 2024 | Syndicated blog