Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 636357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:30:22+00:00 2026-05-13T20:30:22+00:00

MVC2 comes with a nice sample of a validation attribute called PropertiesMustMatchAttribute that will

  • 0

MVC2 comes with a nice sample of a validation attribute called “PropertiesMustMatchAttribute” that will compare two fields to see if they match. Use of that attribute looks like this:

[PropertiesMustMatch("NewPassword", "ConfirmPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public class ChangePasswordModel
{
    public string NewPassword { get; set; }
    public string ConfirmPassword { get; set; }
}

The attribute is attached to the model class and uses a bit of reflection to do its work. You’ll also notice the error message here is specified directly: “The new password and confirmation password do not match.”

If you don’t specify the message, the default message is generated using some code like this (shortened for clarity):

private const string _defaultErrorMessage = "'{0}' and '{1}' do not match.";
public override string FormatErrorMessage(string name)
{
    return String.Format(CultureInfo.CurrentUICulture, _defaultErrorMessage,
        OriginalProperty, ConfirmProperty);
}

The problem with that is that the “OriginalProperty” and “ConfirmProperty” are the hardcoded strings in the attribute – “NewPassword” and “ConfirmPassword” in this example. They don’t actually get the real model metadata (e.g., the DisplayNameAttribute) to put together a more flexible, localizable message. I’d like to have a more generally applicable comparison attribute that uses the metadata display name info and so forth that’s been specified.

Assuming I don’t want to create a custom error message for every instance of my ValidationAttribute, that means I need to get a reference to the model metadata (or, at the very least, the model type that I’m validating) so I can get that metadata information and use it in my error messages.

How do I get a reference to the model metadata for the model I’m validating from inside the attribute?

(While I’ve found several questions that ask about how to validate dependent fields in a model, none of the answers include handling the error message properly.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:30:23+00:00Added an answer on May 13, 2026 at 8:30 pm

    This is actually a subset of the question of how to get the instance decorated by an attribute, from the attribute (similar question here).

    Unfortunately, the short answer is: You can’t.

    Attributes are metadata. An attribute does not know and cannot know anything about the class or member it decorates. It is up to some downstream consumer of the class to look for said custom attributes and decide if/when/how to apply them.

    You have to think of attributes as data, not objects. Although attributes are technically classes, they are rather dumb, because they have a crucial constraint: Everything about them must be defined at compile-time. This effectively means they can’t get access to any runtime information, unless they expose a method that takes a runtime instance and the caller decides to invoke it.

    You could do the latter. You could design your own attribute, and as long as you control the validator, you can make the validator invoke some method on the attribute and have it do pretty much anything:

    public abstract class CustomValidationAttribute : Attribute
    {
        // Returns the error message, if any
        public abstract string Validate(object instance);
    }
    

    As long as whomever uses this class uses the attribute properly, this will work:

    public class MyValidator
    {
        public IEnumerable<string> Validate(object instance)
        {
            if (instance == null)
                throw new ArgumentNullException("instance");
            Type t = instance.GetType();
            var validationAttributes = (CustomValidationAttribute[])Attribute
                .GetCustomAttributes(t, typeof(CustomValidationAttribute));
            foreach (var validationAttribute in validationAttributes)
            {
                string error = validationAttribute.Validate(instance);
                if (!string.IsNullOrEmpty(error))
                    yield return error;
            }
        }
    }
    

    If this is how you consume the attributes, it becomes straightforward to implement your own:

    public class PasswordValidationAttribute : CustomValidationAttribute
    {
        public override string Validate(object instance)
        {
            ChangePasswordModel model = instance as ChangePasswordModel;
            if (model == null)
                return null;
            if (model.NewPassword != model.ConfirmPassword)
                return Resources.GetLocalized("PasswordsDoNotMatch");
            return null;
        }
    }
    

    This is all fine and good, it’s just that the control flow is inverted from what you specify in the original question. The attribute doesn’t know anything about what it was applied to; the validator that uses the attribute has to supply that information (which it can easily do).

    Of course, this isn’t how validation actually works with data annotations in MVC 2 (unless it’s changed significantly since I last looked at it). I don’t think you’ll be able to just plug this in with ValidationMessageFor and other similar functions. But hey, in MVC 1, we had to write all our own validators anyway. There’s nothing stopping you from combining DataAnnotations with your own custom validation attributes and validators, it’s just going to involve a little more code. You’d have to invoke your special validator wherever you write validation code.

    This is probably not the answer you’re looking for, but it’s the way it is, unfortunately; a validation attribute has no way to know about the class it was applied to unless that information is supplied by the validator itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I plan to have 2 projects. 1. mbtech.crm.mvc 2. mbtech.crm.tests I will have a
I have an MVC2 application, and HttpHandler Library. The library, to simplify, serves an
I have a mvc2 application with a view like this <% using (Ajax.BeginForm(UserApprove, new
So i've been working on a MVC2 application just to learn the ropes really.
I have the following asp.net mvc2 template: <%@ Control Language=C# Inherits=System.Web.Mvc.ViewUserControl<ncontinuity2.core.dto.TrainingLookUpContainer> %> <%= Html.EditorFor(x
I am using ASP.NET MVC2 and Entity Framework. I am going to simplify the
I am currently working on an ASP.NET MVC2 project. This is the first time
I am taking some time to learn how to develop asp.net mvc2 websites, but
I'm trying to get my head around using Nunit, Ninject, MVC2 and the ADO.Net
I am building an app with ASP.NET MVC2, Fluent NHibernate, StructureMap, and PostgreSQL. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.