Adding custom entity validation to LightSwitch

December 28, 2011

Adding custom validation is quite easy task, but currently Microsoft has problem with not updated MSDN article http://msdn.microsoft.com/en-us/library/ff852045.aspx. I just would like to tell you that code snippet on this MSDN page is not working.

This is correct snippet (this code is placed in SomeEntity_Validate event on server) :

if (entity.SomeBoolProperty == true)
{
    results.AddPropertyError(“Text with error for user., entity.Details.Properties.SomePropertyToBindErrorTo);
    results.AddEntityError(“Text with error for whole property”);
}

This way you will see error generated from first row of code as your custom error text from first parameter followed by property name. Second row will generate error with name of property followed by your custom text. Errors could look like this :

possible validation messages lightswitch

Please note that this code is fired on server and thus you need to create roundtrip (error will show up when Save was pressed, not on client when filling properties like required fields and so on).

Hope this helps.


Profile picture

Written by Dušan Roštár - the "mr edge case" guy
my twitter : rostacik, my linkedin : rostar, drop me an email : here