ValidationExpression how to use?

May 22, 2011 at 1:40 PM

there is a new property of each model field 'ValidationExpression'

Any examples on how to use it ?

I tried adding a regex expression but could not figure out how to validate.



May 22, 2011 at 3:17 PM

This is kind of hidden I agree. The Entity Framework layer has some additional functionality. There is a "buddy" class associated with each entity. These are metadata classes that describe the data within each entity. EF entities can have a meta class associated with the "System.ComponentModel.DataAnnotations.MetadataType" attribute on the entity class. We are using this to get the regular expression off of the entity and dynamically create a regular express validator in a web UI. There will be an article published on soon. Here is a code snippet of how we are pulling the expression out. It does require reflection.

private string GetRegularExpression(Widgetsphere.EFCore.DataAccess.NHEntityObject entity, Enum field)
var o = entity.GetMetaData();
var a = o.GetType().GetField(field.ToString()).GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.RegularExpressionAttribute), true).FirstOrDefault();
if (a == nullreturn string.Empty;
else return ((System.ComponentModel.DataAnnotations.RegularExpressionAttribute)a).Pattern;

The NHEntityObject is the base object for all entities generated by nHydrate. We take this in and with reflection pull out the RE from the meta class. When we pull this out we create a validation on screen so we do not need to do this in mark-up.

var r1 = new RegularExpressionValidator
ControlToValidate = control.UniqueID,
Display = ValidatorDisplay.None,
ErrorMessage = "The '" + GetDisplayName(e.Entity, e.Field) + "' is not the correct format!",
ValidationExpression = GetRegularExpression(e.Entity, e.Field)

I know this is not a complete description but this should allow you to pull the validation expression out of an entity.

May 22, 2011 at 4:45 PM


so this applies only to EF entities and not NH entities ?

May 22, 2011 at 5:12 PM

Yes. This is an EFDAL feature.