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 3334312
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:53:28+00:00 2026-05-17T23:53:28+00:00

I have a custom validation attribute which checks to see if two properties have

  • 0

I have a custom validation attribute which checks to see if two properties have the same values or not (like password and retype password):

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    public class EqualToPropertyAttribute : ValidationAttribute
    {
        public string CompareProperty { get; set; }

        public EqualToPropertyAttribute(string compareProperty)
        {
            CompareProperty = compareProperty;
            ErrorMessage = string.Format(Messages.EqualToError, compareProperty);
        }

        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return true;
            }
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
            var property = properties.Find(CompareProperty, true);
            var comparePropertyValue = property.GetValue(value).ToString();

            return comparePropertyValue == value.ToString();
        }
    }

I have a view model class which has all the fields for the signup form as follows:

public class SignUpViewModel
    {
        [Required]
        [StringLength(100)]
        public string Username { get; set; }

        [Required]
        [Password]
        public string Password { get; set; }

        [Required]
        [DisplayText("RetypePassword")]
        [EqualToProperty("Password")]
        public string RetypePassword { get; set; }

        [Required]
        [StringLength(50)]
        [DisplayText("FirstName")]
        public string FirstName { get; set; }

        [Required]
        [StringLength(100)]
        [DisplayText("LastName")]
        public string LastName { get; set; }

        [Required]
        [DisplayText("SecurityQuestion")]
        public int SecurityQuestionID { get; set; }

        public IEnumerable<SelectListItem> SecurityQuestions { get; set; }

        [Required]
        [StringLength(50)]
        public string Answer { get; set; }
}

Below is my controller code:

public virtual ActionResult Index()
    {
        var signUpViewModel = new SignUpViewModel();

        signUpViewModel.SecurityQuestions = new SelectList(questionRepository.GetAll(),"SecurityQuestionID", "Question");
        return View(signUpViewModel);
    }

        [HttpPost]
        public virtual ActionResult Index(SignUpViewModel viewModel)
        {
            // Code to save values to database
        }

When I enter the form values and hit submit the line of code which tries to get the property descriptor var property = properties.Find(CompareProperty, true); is returning null. Can anyone help me understand why this is happening?

  • 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-17T23:53:29+00:00Added an answer on May 17, 2026 at 11:53 pm

    Because the object value parameter of IsValid() is not the entire model, but just your string RetypePassword.

    It would need to be an attribute that affects the whole model object, and not just a property.

    Although I would suggest you to use the PropertiesMustMatchAttribute.

    [PropertiesMustMatch("Password", "RetypePassword", 
      ErrorMessage = "The password and confirmation password do not match.")]
    public class SignUpViewModel
    {
       [Required]
       [StringLength(100)]
       public string Username { get; set; }
    
       [Required]
       [Password]
       public string Password { get; set; }
    
       [Required]
       [DisplayText("RetypePassword")]
       public string RetypePassword { get; set; }
    
       //...
    }
    

    Edit

    That attribute is actually not part of the ASP.NET MVC2 framework, but defined in the default MVC 2 project template.

    [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 = new object();
    
        public PropertiesMustMatchAttribute( string originalProperty, string confirmProperty )
            : base( _defaultErrorMessage ) {
            OriginalProperty = originalProperty;
            ConfirmProperty = confirmProperty;
        }
    
        public string ConfirmProperty { get; private set; }
        public string OriginalProperty { get; private set; }
    
        public override object TypeId {
            get {
                return _typeId;
            }
        }
    
        public override string FormatErrorMessage( string name ) {
            return String.Format( CultureInfo.CurrentUICulture, ErrorMessageString,
                OriginalProperty, ConfirmProperty );
        }
    
        public override bool IsValid( object value ) {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties( value );
            object originalValue = properties.Find( OriginalProperty, true /* ignoreCase */).GetValue( value );
            object confirmValue = properties.Find( ConfirmProperty, true /* ignoreCase */).GetValue( value );
            return Object.Equals( originalValue, confirmValue );
        }
    }
    

    On a side note, MVC 3 has a CompareAttribute that does exactly what you want.

    public class SignUpViewModel
    {
        [Required]
        [StringLength(100)]
        public string Username { get; set; }
    
        [Required]
        [Password]
        public string Password { get; set; }
    
        [Required]
        [DisplayText("RetypePassword")]
        [Compare("Password")] // the RetypePassword property must match the Password field in order to be valid.
        public string RetypePassword { get; set; }
    
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom validation attribute derived from action filter attribute. Currently the attribute
I have build my own custom validation framework for WPF using attribute based validation.
Suppose I have this model: public class ViewModel { [Required] public string UserInput {
In ASP.NET MVC 2, I have a Linq to sql class that contains a
The documentation on Core Data entities says: You might implement a custom class, for
We're using xVal and the standard DataAnnotationsValidationRunner described here to collect validation errors from
I'm playing around with writing a jQuery plugin that uses an attribute to define
In a jsp file, is there a way to find a component on that
This question title is likely to be worded poorly so feel free to adjust.
I've been playing with nhibernate.validator and xVal and JQuery and they work together quite

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.