Simple validation of properties in EPiServer 6 using DataAnnotations with PageTypeBuilder

In EPiServer 7 you can easily add validation to pagetype properties with DataAnnotations attribute, like this:

[RegularExpression(@"^([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,9})$"    , ErrorMessage = "Must be valid email address")]

In EPiServer 6 you can create a custom property (involves creating some classes and messing up the db sometimes), or hook up onto global page save validation event and check if property x is valid and return e.message. (http://blog.bigfinger.se/2010/4/26/simple-validation-of-propertyvalues-in-episerver-cms-6.aspx)

Or, you can combine the usage of hooking up onto global pagedata validation with DataAnnotaions support, then you in fact
have something similar to the EPiServer 7 way with DataAnnotations support. Just go here: Validating page data with Data Annotations.
Thank you Stefan Forsberg.

PS:
the GlobalPageValidation_Validators event can be hooked onto through Global.asax.cs like this:

public class Global : EPiServer.Global
{
protected void Application_Start(Object sender, EventArgs e)
{

//Enables validation for pagetype properties with DataAnnotations
GlobalPageValidation.Validators += GlobalPageValidation_Validators;
}

void GlobalPageValidation_Validators(object sender, PageValidateEventArgs e)
{
PageDataValidator validator = new PageDataValidator();
var errors = validator.GetErrors(e.Page).ToList();

if (errors.Any())
{
e.IsValid = false;
foreach (var error in errors)
{
e.ErrorMessage += string.Format("{0} <br />", error);
}

}
}

Also added an <br /> in case of several errors.
Validation Error message looks like this in EPiServer edit mode:

ScreenShot808
Its a bit on the simple side; remember to include which field its connected to in the errortext, no indication is shown next to the particular field.

Now you can just use DataAnnotations validation attributes, like these:

[RegularExpression]
[Required]
[StringLength]
[Range]

Inherit System.ComponentModel.DataAnnotations  and override IsValid to do something more complicated with your validations.

EPiServer Commerce R1 – Trigger Lucene Searching and Indexing

If you want to use certain parts of the Commerce Manager (e.g. Marketing -> Promotion -> “Order: Buy X, get N quantity of Y at a discount”. -> Catalog entries picker, the Commerce default Lucene search indexer must be active.

Else you get this error inside the picker window:

System.IO.FileNotFoundException: no segments* file found in Lucene.Net.Store.FSDirectory

The searchindex creation can be triggered manually through Manager -> Administration -> System settings -> Search Index.

Scheduled jobs should be available in CMS but here is how to trigger the indexing through code:

via Searching and Indexing.

Build index command

SearchManager searchManager = new SearchManager(applicationName);

searchManager.BuildIndex(false);

Set EPiServer Commerce 1 caching on nodes/entries

<?xml version="1.0"?>
<Catalog autoConfigure="true">
  <Connection connectionStringName="EcfSqlConnection" />
  <Cache enabled="true" collectionTimeout="0:1:0" entryTimeout="0:1:0" nodeTimeout="0:1:0" schemaTimeout="0:2:0" />

entries and nodes cache timeout is set to 1 minute.

Changes in Commerce should affect the “productpage” in the website immediately but  sometimes the cache invalidation dont work. The above setting is ultimately the cache timeout then.

 

EPiServer Commerce R2 SP1 available metatypes and their UI representations

List of Commerce meta properties types and their Commerce manager representation UI:ScreenShot671 The meta type is the same as the field-“name” but remove “test” E.g. “datetime”, “integer” type and so on.

All seem to validate in a predictable way, e.g. the email type must be a valid email in the textbox, except the “Url” meta type which doesn’t seem to validate at all(?).

The “file”-type is multi file upload initially.

“imagefile” type has a preview of the image.

“dictionary” doesnt seem to have any remove value function(?).

If invalid data is entered, a red * will appear next to the field and in the top of the page upon save.

A little warning though, this is tested in Chrome web browser which isn’t the best browser when using the Commerce Manager (IE 7 is recommended). Some types might behave behave in a better way in Internet Explorer.

Extra warning: when I added all the different meta fields the “Catalog Node Edit” function stopped working in Commerce Manager UI. Like this:

[FormatException: Input string was not in a correct format.]
   System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +10170355
   System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +207
   Mediachase.Commerce.Manager.Catalog.Tabs.NodeOverviewEditTab.BindMetaForm() +376

There is probably one of the properties that is not working correctly in the Commerce manager UI (buggy). And the function BindMetaForm() just fails.

Lesson learned: mess gently, very gently with commerce meta properties!

More on the subject – How to Create custom metafields controls in the UI: http://sdk.episerver.com/commerce/1.1.1/Content/Developers%20Guide/Architecture/ExtendCustomizeMetaFieldControls.htm

The Nansen blog: Fixing EPiServer’s context menu bug in Google Chrome

As all of you using Google Chrome on an EPiServer website, know that the right click menu isn’t opening correctly in your browser. The problem appears when you have scrolled down on a page and try to open the context menu – the menu will open at the top of the page as if you had not scrolled the page…

via The Nansen blog: Fixing EPiServer’s context menu bug in Google Chrome.