DataAnnotations 3.5 SP1 doesn't support what you're trying to do if the attribute is declared at the property level. However, since you have MVC 2 Beta, take a look at the RegisterModel type (in Models/AccountModels.cs in the default template). It shows
an example of using something like [MustMatch], but declared on the model itself rather than on a property.
Marked as answer by ricka6 on Dec 02, 2009 07:56 PM
Beware that there is small bug in that attribute implementation. Even tho it advertises that it can be used multiple times (AllowMultiple = true), this wont work. Thats because validation attributes discovery depends on TypeDescriptor and for it to see multiple
instances of the same attribute TypeId property should be overriden like this:
public override object TypeId {
get {
return this;
}
}
So if you want to require users to confirm both email and password, you will need to add this override.
Maciej
Don't forget to click "Mark as Answer" on the post(s) that helped you.
Marked as answer by ricka6 on Dec 08, 2009 08:55 PM
I'm trying to do this on model and client side... Will be great if there is possibility to do it in "one place". The "MustMatch" attribute is exactly what I need.
We added this to MVC 2 Beta, but I see that you're using VS 2010, so you still have the older code (since VS 2010 Beta 2 shipped with MVC 2 Preview 2).
This is the attribute:
[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);
}
}
Marked as answer by ricka6 on Dec 29, 2009 06:39 PM
sfev
0 Points
2 Posts
MVC 2 Beta - Validate two properties match
Dec 01, 2009 09:48 PM|LINK
Is there any way of using the built-in validation attributes to validate this model:
public class TestModel { public string Password {get;set;} [MustMatch("Password")] public string ConfirmPassword {get;set;} }Where MustMatchAttribute is a custom ValidationAttribute I've created. It would seem that with the default DataAnnotations classes there isn't?
levib
Star
7702 Points
1099 Posts
Microsoft
Re: MVC 2 Beta - Validate two properties match
Dec 02, 2009 12:14 AM|LINK
DataAnnotations 3.5 SP1 doesn't support what you're trying to do if the attribute is declared at the property level. However, since you have MVC 2 Beta, take a look at the RegisterModel type (in Models/AccountModels.cs in the default template). It shows an example of using something like [MustMatch], but declared on the model itself rather than on a property.
sfev
0 Points
2 Posts
Re: MVC 2 Beta - Validate two properties match
Dec 04, 2009 02:41 PM|LINK
Well that's good enough for me
Thanks a lot for your help
molesinski
Member
314 Points
71 Posts
Re: MVC 2 Beta - Validate two properties match
Dec 04, 2009 08:09 PM|LINK
Beware that there is small bug in that attribute implementation. Even tho it advertises that it can be used multiple times (AllowMultiple = true), this wont work. Thats because validation attributes discovery depends on TypeDescriptor and for it to see multiple instances of the same attribute TypeId property should be overriden like this:
public override object TypeId { get { return this; } }So if you want to require users to confirm both email and password, you will need to add this override.
Maciej
levib
Star
7702 Points
1099 Posts
Microsoft
Re: MVC 2 Beta - Validate two properties match
Dec 05, 2009 12:29 AM|LINK
Molesinski - thanks for the report. I've filed a bug on this.
pigmej
Member
6 Points
3 Posts
Re: MVC 2 Beta - Validate two properties match
Dec 27, 2009 04:27 PM|LINK
Still nothing something like requested ?
I'm using VS 2010 and C#.
I'm trying to do this on model and client side... Will be great if there is possibility to do it in "one place". The "MustMatch" attribute is exactly what I need.
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: MVC 2 Beta - Validate two properties match
Dec 27, 2009 08:47 PM|LINK
The account models include an example of this (server-side only) when you create a new MVC 2 project.
Writing a client-side version should be relatively simple.
pigmej
Member
6 Points
3 Posts
Re: MVC 2 Beta - Validate two properties match
Dec 27, 2009 08:58 PM|LINK
Hmmm.....
There is no something like this :| I've just checked...
1. Models are 'empty'
2. Validation of password and confirmPassword is ONLY in AccountControler in ValidateRegistration function. Nothing based on the properties... etc...
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: MVC 2 Beta - Validate two properties match
Dec 27, 2009 10:24 PM|LINK
We added this to MVC 2 Beta, but I see that you're using VS 2010, so you still have the older code (since VS 2010 Beta 2 shipped with MVC 2 Preview 2).
This is the attribute:
[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); } }pigmej
Member
6 Points
3 Posts
Re: MVC 2 Beta - Validate two properties match
Dec 27, 2009 11:54 PM|LINK
Thanks, anyway is there any way to have beta 2 in VS 2010 ? Normal install of Beta 2 from http://www.microsoft.com/downloads/details.aspx?FamilyID=4817cdb2-88ea-4af4-a455-f06b4c90fd2c&displaylang=en will work ?