Mismatch Validation

Jul 18, 2010 at 6:14 PM
Edited Jul 18, 2010 at 6:17 PM

I am wondering what would be the easiest way to use the new ModelMetadataConfiguration to configure a mismatch validation rule.
Here is my model

 public class SignUpModel
{
   public string UserName { get; set; }
   public string Password { get; set ; }
   public string ConfirmPassword { get; set ; }
}

 
public class SignUpModelConfiguration : ModelMetadataConfiguration<SignUpModel>
{
 
  public SignUpViewDataConfiguration ()
  {
    Configure(model => model.UserName).DisplayName(Resources.Account_UserName )
                                      .Expression(@"^.{4,}$", Resources.Error_Min4CharRequired); 


     Configure(model => model.Password).DisplayName(Resources.Account_Password)
                                      .AsPassword()
                                      .Expression(@"^.{4,}$", Resources.Error_Min4Required);

   Configure(model => model.ConfirmPassword).DisplayName(Resources.Account_PasswordConfirm)
                                           .Expression("??????????????", Resources.Error_Mismach); 


   }
}

What kind of expresssion should I put  here  .Expression("??????????????", Resources.Error_Mismach)
I struggled with different regular expressions values and failed.
Maybe there is another easy solution.

I am looking for a mismach error when Password and ConformPassword do not match

Thank you for your help


 

 

 

Coordinator
Jul 18, 2010 at 10:16 PM

The default DataAnnotations attributes currently does not have any support for Password matching (behind the scene the MvcExtensions also uses the same), if you check the default asp.net mvc project you will find that it uses a custom attribute (class name something like PropertyShouldMatch). I think you can create PropertyMatchingValidationMetadata and PropertyMatchingValidator classes and and add extension methods for ModelMetadataItemBuilder to hook those.