The Problem Solver

Tell me and I will forget
Show me and I will remember
Involve me and I will understand
- Confucius -

Validating custom activities in the Workflow designer

Create a derived class from ActivityValidator and add it to your custom activity using the ActivityValidatorAttribute. For example:

[ActivityValidator(typeof(ValidatedActivityValidator))]
public partial class ValidatedActivity : SequenceActivity
{
}

The validator class now look like this:

class ValidatedActivityValidator : ActivityValidator
{
    public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
    {
        ValidationErrorCollection result = base.Validate(manager, obj);

        ValidatedActivity activity = obj as ValidatedActivity;
        if (activity != null && activity.Parent != null)
        {
            if (activity.AValueBetween1And250 < 1)
                result.Add(
                    new ValidationError("The value is to small", 
                        102, false, "AValueBetween1And250"));

            if (activity.AValueBetween1And250 > 250)
                result.Add(
                    new ValidationError("The value is to big", 
                        103, true, "AValueBetween1And250"));
        }

        return result;
    }
}

Note that the object parameter (obj) is the activity to be validated.

The ActivityValidator type contains two virtual function Validate and ValidateProperties. It doesn't really matter which you use as the Validate actually calls the ValidateProperties function. This means they are both called at the same time unless you change the behavior by not calling the base.Validate() function.

To indicate errors or warnings you can add a ValidationError object to the ValidationErrorCollection to be returned. Adding the property name makes life easy on the developer using your activity because it allows the designer to automatically select the property by double clicking the error.

Another thing to keep in mind is that the validator will be called when the activity itself is compiles. To see if this is the case check the activity Parent class which will be null.

The validator is also called when the workflow is executed allowing for runtime property checking.

Enjoy!

www.TheProblemSolver.nl
http://wiki.WindowsWorkflowFoundation.eu

Published Sun, May 18 2008 16:28 by Maurice
Filed under: , , ,

Comments

# Validating workflow activity metadata@ Tuesday, May 20, 2008 7:31 AM

There are several options for validating properties on a custom activity. Besides the possibility of

# Validating workflow activity metadata@ Tuesday, May 20, 2008 7:54 AM

There are several options for validating properties on a custom activity. Besides the possibility of

# re: Validating custom activities in the Workflow designer@ Tuesday, November 25, 2008 3:19 AM

I created a custom validator class for my custom activity but Error messages are displayed in custom activity as well as Main parent workflow designer. i need to display error messages only for my custom activity.

Thanx in advance.

by Kumesh