Navigation:  I Want To... > Basics >

Add validation to an application component

Previous pageReturn to chapter overviewNext page

Use Case

You want to validate your application component's properties and provide some visual cue to the user (in the view) indicating what needs to be done to correct the value(s).

Relevant Architecture

Application Components
Designing an Application Component

Relevant Types

ValidationAttribute
ValidateRegexAttribute
ValidateGreaterThanAttribute
ValidateLessThanAttribute
ValidateNotNullAttribute
ValidateLengthAttribute
ValidationMethodForAttribute
IValidationRule

Sample Code

using System;
using System.IO;
using ClearCanvas.Common;
using ClearCanvas.Desktop;
using ClearCanvas.Desktop.Actions;
using ClearCanvas.Desktop.Tools;
using ClearCanvas.Desktop.Validation;
 
namespace MyPlugin.TextEditor
{
   [ExtensionPoint()]
   public class TextEditorComponentViewExtensionPoint : ExtensionPoint<IApplicationComponentView> {}
 
   [AssociateView(typeof (TextEditorComponentViewExtensionPoint))]
   public class TextEditorComponent : ApplicationComponent
   {
      private string _filename;
      private string _text;
      private int _wordCount;
 
      public TextEditorComponent()
      {
      }
 
      [ValidateRegex("[<>]", SuccessOnMatch = false, Message = "Cannot have < or > characters.")]
      public string Filename
      {
         get { return _filename; }
         set
         {
            _filename = value;
            this.Modified = true;
         }
      }
 
      public string Text
      {
         get { return _text; }
         set
         {
            _text = value;
            this.Modified = true;
            UpdateWordCount();
         }
      }
 
      public int WordCount
      {
         get { return _wordCount; }
         private set
         {
            _wordCount = value;
            NotifyPropertyChanged("WordCount");
         }
      }
 
      public void Save()
      {
         if (base.HasValidationErrors)
         {
            base.ShowValidation(true);
         }
         else
         {
            File.WriteAllText(_filename, _text);
            this.Exit(ApplicationComponentExitCode.Accepted);
         }
      }
 
      public void Cancel()
      {
         this.Exit(ApplicationComponentExitCode.None);
      }
 
      private void UpdateWordCount()
      {
         // really simple algorithm to count words for illustrative purposes only
         // by assuming spaces, period, comma, or (semi)colon between words
         char[] wordSeparators = new char[] {' ', '.', ',', ';', ':'};
         int wordCount = this.Text.Split(wordSeparators, StringSplitOptions.RemoveEmptyEntries).Length;
         this.WordCount = wordCount;
      }
   }
}

Remarks

The above code sample is based on the Adding validation sub-section in Designing an Application Component.  Please consult the aforementioned section for a more thorough discussion on application component validation.

Examples in Code Base

ClearCanvas.ImageViewer.Configuration.ServerTree.DicomServerEditComponent