Today I Learned

Today I Learned: How To Add a Custom Validation Rule To DevExpress’s Validation Provider

So today, I was working on a login form at work that has a Username text box and a Password text box. So far, so good. As most things though, there are some business rules to show particular validation errors before the user clicks the login button.


When I designed the form originally, I used DevExpress’ WinForm controls to make it a bit more sleek. With that being said, I was also using their validation provider to show inline error messages for the controls.

During the development, I had used DevExpress’s built-in custom validation rules to say that the password could not be empty and if it was, show this particular error message. This worked great, until someone entered a password that was just a space. Then the application crashed because the presenter was asked to do some work with an invalid password.

Looking back at the built-in rule, there wasn’t an easy way to handle a password that consisted of all blanks, but there could 1 or more of them.

After digging around the documentation, it looked like I should create a custom Validation Rule.

So the first step is to create a new class that inherits from ValidationRule

After creating this rule, the next step is to override the Validate method and add my custom validation logic here

Now that the custom rule is defined, I needed to hook it up to the password text box

Because of this, whenever the control is being validated, the control will use the CustomPasswordValidationRule class to run the new logic which is awesome. However, it will use a default error message and icon which is less than ideal.

To get around that, we can set some properties as part of the constructor for the class. I could have set the properties as part of setting up the validation provider, but none of the details change. Either it validates with no problems or it should show this particular error message with this particular error icon.

What’s really great about this solution is that now I can test that the validation logic using unit tests where before I would have had to write UI tests.

Leave a Reply

%d bloggers like this: