Is the Compare Validator Bugged

2019-08-04 02:50发布

问题:

This is very basic but it always returns false on the compare validation. Anyone else running in to this problem?

 public class UsersRegisterUserViewModel
{
    [DisplayName("E-Mail Address")]
    [Required(ErrorMessage = "E-Mail Address is required")]
    [RegularExpression(@"^[A-Za-z0-9_\-\.]+@(([A-Za-z0-9\-])+\.)+([A-Za-z\-])+$", ErrorMessage = "Invalid E-mail Address")]
    public string RegUsername { get; set; }

    [Required]
    [Display(Name = "Password")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Required]
    [Display(Name = "Confirm Password")]
    [Compare("Password", ErrorMessage = "Passwords must match")]
    [DataType(DataType.Password)]
    public string RegConfirmPassword { get; set; }
}

回答1:

adapters.add("equalto", ["other"], function (options) {
    var prefix = getModelPrefix(options.element.name),
        other = options.params.other,
        fullOtherName = appendModelPrefix(other, prefix),
        //element = $(options.form).find(":input[name=" + fullOtherName + "]")[0];
        element = $(options.form).find(":input[name='" + fullOtherName + "']")[0];


回答2:

MVC3 Compare attribute is buggy when comparing passwords independently of Account Controller. It seems it is hardcoded to only work with Account controller.
1. Cut and past email, password, confirm password from RegisterModel into a new file called ViewModels/ShortRegister.cs 2. Cut razor code ( email, password, confirm password) from register view and past it into partial view, call it "_shortRegistration". 3. Create a new controller called "ShortRegistration". Add the partial view into ShortRegistation. 5. Add related jquery scripts

  1. Create a link on home page to ShortRegistration.
  2. Confirmation error message always fires error message.
  3. Remove the email from the partial view confirmation, The Compare functionality works.
  4. Add userName to the partial view and view-model, Compare functionality fails, again password confirmation error message always displays error message.

Has this bug been fixed? I disabled Compare attribute and wrote jquery and CCS to fix this! I am more than happy to email the code to prove that Compare is buggy.



回答3:

Hmm, no, I am not running into such problems. I've just tested the following code and it worked perfectly fine as expected.

Model:

public class UsersRegisterUserViewModel
{
    [DisplayName("E-Mail Address")]
    [Required(ErrorMessage = "E-Mail Address is required")]
    [RegularExpression(@"^[A-Za-z0-9_\-\.]+@(([A-Za-z0-9\-])+\.)+([A-Za-z\-])+$", ErrorMessage = "Invalid E-mail Address")]
    public string RegUsername { get; set; }

    [Required]
    [Display(Name = "Password")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Required]
    [Display(Name = "Confirm Password")]
    [Compare("Password", ErrorMessage = "Passwords must match")]
    [DataType(DataType.Password)]
    public string RegConfirmPassword { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new UsersRegisterUserViewModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(UsersRegisterUserViewModel model)
    {
        return View(model);
    }
}

View:

@model UsersRegisterUserViewModel

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.RegUsername)
        @Html.EditorFor(x => x.RegUsername)
        @Html.ValidationMessageFor(x => x.RegUsername)
    </div>

    <div>
        @Html.LabelFor(x => x.Password)
        @Html.EditorFor(x => x.Password)
        @Html.ValidationMessageFor(x => x.Password)
    </div>

    <div>
        @Html.LabelFor(x => x.RegConfirmPassword)
        @Html.EditorFor(x => x.RegConfirmPassword)
        @Html.ValidationMessageFor(x => x.RegConfirmPassword)
    </div>

    <input type="submit" value="OK" />
}

So now the question becomes: how is your code different than mine?