Using DataAnnotations to compare two model properties

Make sure that your project references system.web.mvc v3.xxxxx. Then your code should be something like this: using System.Web.Mvc; . . . . [Required(ErrorMessage = “This field is required.”)] public string NewPassword { get; set; } [Required(ErrorMessage = “This field is required.”)] [Compare(nameof(NewPassword), ErrorMessage = “Passwords don’t match.”)] public string RepeatPassword { get; set; }

Validate data using DataAnnotations with WPF & Entity Framework?

You can use the DataAnnotations.Validator class, as described here: http://johan.driessen.se/archive/2009/11/18/testing-dataannotation-based-validation-in-asp.net-mvc.aspx But if you’re using a “buddy” class for the metadata, you need to register that fact before you validate, as described here: http://forums.silverlight.net/forums/p/149264/377212.aspx TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(myEntity), typeof(myEntityMetadataClass)), typeof(myEntity)); List<ValidationResult> results = new List<ValidationResult>(); ValidationContext context = new ValidationContext(myEntity, null, null) bool valid = Validator.TryValidateObject(myEntity, context, … Read more

IValidatableObject Validate method firing when DataAnnotations fails

Considerations after comments’ exchange: The consensual and expected behavior among developers is that IValidatableObject‘s method Validate() is only called if no validation attributes are triggered. In short, the expected algorithm is this (taken from the previous link): Validate property-level attributes If any validators are invalid, abort validation returning the failure(s) Validate the object-level attributes If … Read more

Is the DataTypeAttribute validation working in MVC2?

[DataType(“EmailAddress”)] doesn’t influence validation by default. This is IsValid method of this attribute (from reflector): public override bool IsValid(object value) { return true; } This is example of custom DataTypeAttribute to validate Emails (taken from this site http://davidhayden.com/blog/dave/archive/2009/08/12/CustomDataTypeAttributeValidationCustomDisplay.aspx): [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] public class EmailAddressAttribute : DataTypeAttribute { private readonly Regex regex = … Read more

Get [DisplayName] attribute of a property in strongly-typed way

There are two ways to do this: Models.Test test = new Models.Test(); string DisplayName = test.GetDisplayName(t => t.Name); string DisplayName = Helpers.GetDisplayName<Models.Test>(t => t.Name); The first one works by virtue of writing a generic extension method to any TModel (which is all types). This means it will be available on any object and not just … Read more

MetadataType problem

EDIT: I found the answer here: http://forums.silverlight.net/forums/p/149264/377212.aspx Before validating, you need to manually register the metadata class: TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Person), typeof(PersonMetadata)), typeof(Person)); List<ValidationResult> res = new List<ValidationResult>(); bool valid = Validator.TryValidateObject(p, new ValidationContext(p, null, null), res, true); (Original answer follows) The problem isn’t specifically with your partial class, it’s that Validator.TryValidateObject doesn’t seem to recognize … Read more

Writing a CompareTo DataAnnotation Attribute

Check The AccountMOdel in the default project of MVC2, There is an attribute PropertiesMustMatchAttribute applied to the ChangePasswordModel to validate that the NewPassword and ConfirmPassword Match [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] public sealed class PropertiesMustMatchAttribute : ValidationAttribute { private const string _defaultErrorMessage = “‘{0}’ and ‘{1}’ do not match.”; private readonly object _typeId … Read more