MVC 2 Beta - Validate two properties matchhttp://forums.asp.net/t/1499238.aspx/1?MVC+2+Beta+Validate+two+properties+matchWed, 30 Dec 2009 02:48:37 -050014992383542554http://forums.asp.net/p/1499238/3542554.aspx/1?MVC+2+Beta+Validate+two+properties+matchMVC 2 Beta - Validate two properties match <p>Is there any way of using the built-in validation attributes to validate this model:<br> <br> </p> <p></p> <pre>public class TestModel { public string Password {get;set;} [MustMatch(&quot;Password&quot;)] public string ConfirmPassword {get;set;} } </pre> <p>Where MustMatchAttribute is a custom ValidationAttribute I've created.&nbsp; It would seem that with the default DataAnnotations classes there isn't?</p> 2009-12-01T21:48:53-05:003542653http://forums.asp.net/p/1499238/3542653.aspx/1?Re+MVC+2+Beta+Validate+two+properties+matchRe: MVC 2 Beta - Validate two properties match <p>DataAnnotations 3.5 SP1 doesn't support what you're trying to do if the attribute is declared at the property level.&nbsp; However, since you have MVC 2 Beta, take a look at the RegisterModel type (in Models/AccountModels.cs in the default template).&nbsp; It shows an example of using something like [MustMatch], but declared on the model itself rather than on a property.<br> </p> 2009-12-02T00:14:22-05:003548310http://forums.asp.net/p/1499238/3548310.aspx/1?Re+MVC+2+Beta+Validate+two+properties+matchRe: MVC 2 Beta - Validate two properties match <p>Well that's good enough for me</p> <p><br> </p> <p>Thanks a lot for your help<br> </p> 2009-12-04T14:41:38-05:003548814http://forums.asp.net/p/1499238/3548814.aspx/1?Re+MVC+2+Beta+Validate+two+properties+matchRe: MVC 2 Beta - Validate two properties match <p>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:</p> <p></p> <pre class="prettyprint">public override object TypeId { get { return this; } }</pre> <p>So if you want to require users to confirm both email and password, you will need to add this override.</p> <p>Maciej</p> <p></p> 2009-12-04T20:09:49-05:003549019http://forums.asp.net/p/1499238/3549019.aspx/1?Re+MVC+2+Beta+Validate+two+properties+matchRe: MVC 2 Beta - Validate two properties match <p>Molesinski - thanks for the report.&nbsp; I've filed a bug on this.<br> </p> 2009-12-05T00:29:34-05:003584579http://forums.asp.net/p/1499238/3584579.aspx/1?Re+MVC+2+Beta+Validate+two+properties+matchRe: MVC 2 Beta - Validate two properties match <p>Still nothing something like requested ?</p> <p><br> </p> <p>I'm using VS 2010 and C#.</p> <p><br> </p> <p>I'm trying to do this on model and client side... Will be great if there is possibility to do it in &quot;one place&quot;. The &quot;MustMatch&quot; attribute is exactly what I need.</p> 2009-12-27T16:27:23-05:003584731http://forums.asp.net/p/1499238/3584731.aspx/1?Re+MVC+2+Beta+Validate+two+properties+matchRe: MVC 2 Beta - Validate two properties match <p><font face="Calibri" size="2" color="black"><span dir="ltr" style="font-size:16px">The account models include an example of this (server-side only) when you create a new MVC 2 project.</span></font></p> <p><font face="Calibri" size="2" color="black"><span dir="ltr" style="font-size:16px">Writing a client-side version should be relatively simple.</span></font></p> 2009-12-27T20:47:04-05:003584742http://forums.asp.net/p/1499238/3584742.aspx/1?Re+MVC+2+Beta+Validate+two+properties+matchRe: MVC 2 Beta - Validate two properties match <p>Hmmm.....</p> <p><br> </p> <p>There is no something like this :| I've just checked...</p> <p>1. Models are 'empty'</p> <p>2. Validation of password and confirmPassword is ONLY in AccountControler in ValidateRegistration function. Nothing based on the properties... etc...</p> 2009-12-27T20:58:59-05:003584777http://forums.asp.net/p/1499238/3584777.aspx/1?Re+MVC+2+Beta+Validate+two+properties+matchRe: MVC 2 Beta - Validate two properties match <p>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).</p> <p>This is the attribute:</p> <p><pre class="prettyprint">[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] public sealed class PropertiesMustMatchAttribute : ValidationAttribute { private const string _defaultErrorMessage = &quot;'{0}' and '{1}' do not match.&quot;; 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); } }</pre><br> <br> </p> 2009-12-27T22:24:40-05:003584808http://forums.asp.net/p/1499238/3584808.aspx/1?Re+MVC+2+Beta+Validate+two+properties+matchRe: MVC 2 Beta - Validate two properties match <p>Thanks, anyway is there any way to have beta 2 in VS 2010 ? Normal install of Beta 2 from <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=4817cdb2-88ea-4af4-a455-f06b4c90fd2c&amp;displaylang=en"> http://www.microsoft.com/downloads/details.aspx?FamilyID=4817cdb2-88ea-4af4-a455-f06b4c90fd2c&amp;displaylang=en</a>&nbsp;will work ?</p> 2009-12-27T23:54:31-05:003584838http://forums.asp.net/p/1499238/3584838.aspx/1?Re+MVC+2+Beta+Validate+two+properties+matchRe: MVC 2 Beta - Validate two properties match <p>Nope. VS 2010 Beta 2 is stuck with MVC 2 Preview 2.<br> </p> 2009-12-28T01:01:47-05:003588225http://forums.asp.net/p/1499238/3588225.aspx/1?Re+MVC+2+Beta+Validate+two+properties+matchRe: MVC 2 Beta - Validate two properties match <p>See Phils <a href="http://haacked.com/archive/2009/12/19/aspnetmvc-2-and-vs2010.aspx"> Blog ASP.NET MVC 2 and Visual Studio 2010 </a></p> <p>ASP.NET MVC 2 RC is not supported on machines with Visual Studio 2010 Beta 2, Im really referring to the tooling.<br> <br> The ASP.NET MVC 2 RC runtime is fully supported with the ASP.NET 4 runtime</p> 2009-12-29T18:49:42-05:003588699http://forums.asp.net/p/1499238/3588699.aspx/1?Re+MVC+2+Beta+Validate+two+properties+matchRe: MVC 2 Beta - Validate two properties match <p>Just be aware that you will potentially might VS 2010 if you install a later version of MVC 2. Compatible with the runtime != compatible with Visual Studio. :)<br> </p> 2009-12-30T02:48:37-05:00