FrEaKmAn
FrEaKmAn

Reputation: 1845

MVC Compare attribute not working on server-side

I have following code

[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 8)]
[Display(Name = "Password")]
[DataType(DataType.Password)]
public string Password { get; set; }

[Required]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 8)]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }

When I show the form, the client-side validation works but when I test it on server side, it's always valid (tried with Password=pass1234 and ConfirmPassword=nonmatchingpassword)

I also tried with some other attributes like EqualTo in http://foolproof.codeplex.com/ but same problem.

I have included method

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterViewModel model) 
{
    if (WebSecurity.IsAuthenticated)
    {
        return RedirectToAction("Index", "Home");
    }

    if (ModelState.IsValid) 
    {
        // register user...
    }

    model.Countries = this.Countries.FindAll().OrderBy(c => c.Name);
    return View(model);
}

and this is how I render it

@using (Html.BeginForm(null, null, FormMethod.Post, new { @class = "form", @id = "register-form" }))
{
@Html.AntiForgeryToken()
<ul class="form-fields">
    ...
    <li class="form-field">
        @Html.LabelFor(m => m.Password)
        @Html.ValidationMessageFor(m => m.Password)
        @Html.EditorFor(m => m.Password)
    </li>
    <li class="form-field">
        @Html.LabelFor(m => m.ConfirmPassword)
        @Html.ValidationMessageFor(m => m.ConfirmPassword)
        @Html.EditorFor(m => m.ConfirmPassword)
    </li>
    ...     
    <li class="form-actions"> 
        <button class="submit">register</button>
    </li>
</ul>
}

Any ideas what could be wrong? Im using MVC4 RC.

Upvotes: 2

Views: 2267

Answers (3)

keithl8041
keithl8041

Reputation: 2403

A bit late to the party, but I only just ran into a similar issue. This is being caused by an error in the jquery unobstrusive javascript file. A later version will fix it, I just ran

Install-Package jQuery.Validation.Unobtrusive

which installed v2, which works fine for me. Your mileage may vary.

This question has been properly answered here.

Upvotes: 0

Forty-Two
Forty-Two

Reputation: 7605

This is how Microsoft does it in their default MVC project templates:

    [PropertiesMustMatch( "Password", "ConfirmPassword", ErrorMessage = "The                    password and confirmation password do not match." )]
    public class RegisterModel
    {
        [Required]
        [DisplayName( "User name" )]
        public string UserName { get; set; }

        [Required]
        [DataType( DataType.EmailAddress )]
        [DisplayName( "Email address" )]
        public string Email { get; set; }

        [Required]
        [ValidatePasswordLength]
        [DataType( DataType.Password )]
        [DisplayName( "Password" )]
        public string Password { get; set; }

        [Required]
        [DataType( DataType.Password )]
        [DisplayName( "Confirm password" )]
        public string ConfirmPassword { get; set; }
}

I'm not sure exactly how you're doing your server side testing. If this doesn't work for you, perhaps you could post the test that fails?

Upvotes: 1

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93424

Take the StringLength out on the second item. You don't need it because the first item already has it.

Upvotes: 0

Related Questions