Can't figure out how to do this, basically I want to create a custom Validation class which will allow me to define custom code like below:
namespace PhotoManagement.EntityData { [MetadataTypeAttribute(typeof(User.UserMetadata))] public partial class User : BaseEntity { public void MetaSetUp() { TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(User), typeof(UserMetadata)), typeof(User)); } internal sealed class UserMetadata { [CompareString("Hash", ErrorMessage = "Passwords don't match!")] //Custom Validation public string passCheck { get; set; } } } }
I have created a class which I believe is created correctly, however I can't seem to use it in the way I want to above, of course the below class doesn't do much at the moment, just testing to get it working first then will make it do what I want it to do.
namespace PhotoManagement { public class CompareString : ValidationRule { private String _errorMessage = String.Empty; public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; } } public override ValidationResult Validate(object value, CultureInfo cultureInfo) { var str = value as string; MessageBox.Show(str); if (String.IsNullOrEmpty(str)) { return new ValidationResult(true, this.ErrorMessage); } return new ValidationResult(true, null); } } }