Error Codes

Built-in Validation Error Codes

Every built-in validation rule has a defined error code, so you can identify what failure occurred without resorting to string comparisons (which will scale poorly if you're using TNValidate's multi-language support). The ValidationErrorCode class defines all of the error codes. Here is an example.

var Validate = new Validator();
Validate.That(42, "GrowthPercentage").IsGreaterThan(100);
if (Validate.ValidatorResults[0].ErrorCode == ValidationErrorCode.NumericIsGreaterThan)
    Console.WriteLine("Negative growth!"); // This will print.

Setting An Error Code

You may already have a bunch of error codes that you need to adhere to. In that case, use the WithErrorCode(n) method before a validation rule to set the code.

var Validate = new Validator();
Validate.That(42, "GrowthPercentage").WithErrorCode(112).IsGreaterThan(100);
if (Validate.ValidatorResults[0].ErrorCode == 112)
    Console.WriteLine("Negative growth!"); // This will print.

Last edited Aug 26, 2009 at 3:35 PM by jnthn, version 3

Comments

No comments yet.