Custom Validation methods

Custom Validation methods can be provided for every model element in the domain model to validate certain specific constraints. E.g., we will create a validation method for a model element called "EndState". To do that, we need to create a new C# source file and add the following class definition to that file:

namespace *LanguageNamespace*
{
[ModelValidationState(ModelValidationState.Enabled)]
public partial class EndState
{
}

}

The attribute "[ModelValidationState(ModelValidationState.Enabled)]" enables validation for the EndState class, which notifies the validation process to look for validation methods in that class.

Now we can proceed in creating a custom validation method in our EndState class:

namespace *LanguageNamespace*
{
[ModelValidationState(ModelValidationState.Enabled)]
public partial class EndState
{
[ModelValidationMethod(ModelValidationCategories.Open | ModelValidationCategories.Save | ModelValidationCategories.Menu)]
public virtual void Validate(ModelValidationContext context)
{
context.AddMessage(new ModelValidationMessage("MessageId", ModelValidationViolationType.Error, "Message description", this));
}
}

}

The attribute "ModelValidationMethod" specifies when the validation method will be called, which can be whenever

  • the model is opened ("ModelValidationCategories.Open")
  • the model is saved ("ModelValidationCategories.Save")
  • the validation is started from the menu ("ModelValidationCategories.Menu")

A new validation message is added by using the ModelValidationMessage class, which can define a validation message as

  • Error ("ModelValidationViolationType.Error")
  • Warning ("ModelValidationViolationType.Warning")
  • Information ("ModelValidationViolationType.Message")

Every validation message contains a MessageId, which is later on used to filter specific messages that the user might not be interested in.

Last edited Jan 10, 2011 at 3:59 PM by evgenij84, version 5

Comments

No comments yet.