I am creating a custom property validation which should use the error string in the specified resource file when throwing a ValidationException. Am I right in assuming that if I throw an exception and don’t specify the error string in the parameters, it should use the ErrorMessageResourceName and ErrorMessageResourceType I specified in the constructor?
using System;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
namespace OurProduct.Util
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class UniquenessAttribute : ValidationAttribute
{
public UniquenessAttribute()
{
ErrorMessageResourceName = "EmailUniqueError";
ErrorMessageResourceType = typeof (Resources.OurThing);
}
protected override ValidationResult IsValid(object value)
{
throw new ValidationException();
}
}
}
The following should work, provided
OurThingpoints to the generated strongly-typed resource class, for looking up localized strings, etc, and"EmailUniqueError"is one of the static properties of this class:Also make sure you’re validating all properties (if you use TryValidateObject for example, make sure the last parameter is set to true) when doing your tests.