Search code examples
c#nancy

Cannot compare/validate passwords in view model in Nancy project?


I have the following view model:

using System.ComponentModel.DataAnnotations;

    namespace Reflect.Web.Models.ViewModels
    {
        public class ManageUserAccountViewModel
        {
            public string Username { get; set; }
            public string Email { get; set; }
            public float UtcOffset { get; set; }
            public string Password { get; set; }
            [Compare("Password", ErrorMessage = "Passwords don't match.")]
            public string PasswordConfirm { get; set; }
            public int HourOfDay { get; set; }
    }
}

In one of my modules I call the following code:

var userModel = this.Bind<ManageUserAccountViewModel>();
var result = this.Validate(userModel);

Some how the validation result always returns IsValid == true, even when the valjues to be compared obviously are not. What am I missing?

Thanks!


Solution

  • It seems that the Compare attribute is not yet supported in Nancy. If you look here, you can see that there is not CompareValidatorAdapter defined.

    You must implement your own for now (and if you do, just send it as a pull request, so the rest of the world can use it too).