Changing Property Name

By default, the error message will split a camel or pascal case property name. For the validation expression below.
Check(c => c.LastName).Required();

The error message would be "Last Name is required.". To override this default behavior:
Check(c => c.LastName, "Contact Last Name").Required();

And the error message would be "Contact Last Name is required.".

Changing Error Message for Rules

For Required fields

Check(p => p.FirstName).Required("Yo! You needa firsta name!").And.IsAlpha();

For Validators

Check(p => p.FirstName).Required().And.IsAlpha().With( m => m.Message = "First name should only be letters");

The Message overrides the default Error message for the IsAlpha rule only.

If you want to reuse or localize your message, you won't won't to hardcode the string in your Specification, but pull it from the Message Store. You do this by providing the Message Key
Check(p => p.FirstName).Required().And.IsAlpha().With( m => m.MessageKey = "MissingFirstName!");

Formatting Error Message

Coming Soon

Overriding Default Messages

If you need to only change some of the default messages, you can override the default messages in another MessageStore.
  1. Create a resource file in your project.
  2. Find the Resource Key from the RuleErrorMessages.resx file embedded in SpecExpress, either looking at the source or using Reflector. Add a resource with the key name of the resource you want to override
  3. Register your new Resource MessageStore with the ValidationCatalog:
ValidationCatalog.Configure(x => x.AddMessageStore(new ResourceMessageStore(MyRuleErrorMessages.ResourceManager), "MyMessageStore"));

Now you're customized error message will be used instead of the default.

Last edited Jun 14, 2010 at 3:54 PM by TechnoAg, version 5


MBDTeam Jun 24, 2011 at 8:44 AM 
Small Tip for VB users. the line :
Check(p => p.FirstName).Required().And.IsAlpha().With( m => m.Message = "First name should only be letters");
translates in VB to:
Check(Function(p) p.FirstName).Required().[And].IsAlpha().[With](Function(m) InlineAssignHelper(m.Message, "First name should only be letters"))

With the following Function:

Public Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return value
End Function
If you are using .NET 3.5 you cannot use anonymous delegates (in VB.NET)