Channels ▼


Silverlight's Validation Fix

The bulk of validation work is done in the default indexer property. It's called by controls that are bound to object properties as data changes to see if a particular bound property is valid or not. The Person class above has two basic properties in it including Name and Age that need to be validated. This can be done in the default indexer property added into the class as a result of implementing IDataErrorInfo as in Listing 2.

public string this[string propertyName]
        _Errors = null;
            case "Name":
                if (string.IsNullOrEmpty(Name))
                    _Errors = _ErrorsText;
                    return "Name cannot be empty!";
            case "Age":
                if (Age < 1)
                    _Errors = _ErrorsText;
                    return "Age must be greater than zero.";
        return null;

Listing 2: Validating properties using IDataErrorInfo 's default indexer property.

The validation operations performed in the default indexer property are quite simple in this example. First, a switch statement checks which property needs to be validated. From there, each case statement is used to perform validation logic including checking that the Name property isn't null or empty and that the Age property has a value greater than 0. What's nice about this approach is that the validation logic is put in one place and in more real-world situations the logic can even be moved to an external validation object that provides some re-useable methods to check for null strings, check string lengths, validate numeric ranges, etc.

The end result of implementing the IDataErrorInfo interface is that controls bound to properties can check to see if data is valid or not and then display an error message as appropriate. This is done by adding the

ValidatesOnDataErrors property to the data binding as shown next:

<TextBox Text="{Binding Name,Mode=TwoWay,ValidatesOnDataErrors=true}" 
    VerticalAlignment="Top"    />

Figure 1 shows an example of a TextBox control bound to a property containing invalid data. The error message displayed in red is generated automatically as the control checks the default indexer property from IDataErrorInfo of the bound object to verify if a given property is valid or not. This type of validation functionality exists in many controls throughout Silverlight such as TextBox, DataForm, DataGrid, etc.

[Click image to view at full size]
Figure 1: The IDataErrorInfo interface in action.

The downside of the IDataErrorInfo interface is that it doesn't provide a way to perform asynchronous validation. In some situations you may need to call back to a server to check that a user ID or email address is unique within the system or that a tax code matches an existing code stored in a database. IDataErrorInfo doesn't support this type of scenario directly. If you want to get a jump on the process of implementing INotifyDataErrorInfo check out the code associated with this article which is available here.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.