Greg
Greg

Reputation: 2690

MVC Validation not working as expected

not sure if this is to be expected but it was a surprise to me..

I am strongly typing a view with RegistrationViewModel, but when I attempt to submit the form, the validation incorrectly validates in 2 places.

  1. The email field doesnt throw a validation error when I enter "Test".
  2. The password validator says that the passwords dont match when they do.

Anyone know why this is happening?

ViewModel class:

 public class RegistrationViewModel
{
    public RegisterModel RegistrationData { get; set; }
    ...     
}

The validation attributes are on the properties of the RegistrationData class:

public class RegisterModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

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

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

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The Password and Confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [Required]
    [Display(Name = "First name")]
    [StringLength(20, ErrorMessage = "First name must be between 2 and 20 characters.", MinimumLength = 2)]
    public string FirstName { get; set; }

    [Required]
    [Display(Name = "Last name")]
    [StringLength(20, ErrorMessage = "Last name must be between 2 and 20 characters.", MinimumLength = 2)]
    public string LastName { get; set; }
}

View:

@model PropertyManager_MVC.Areas.Account.ViewModels.RegistrationViewModel
<div class="editor-field">
            @Html.TextBoxFor(m => m.RegistrationData.Email)
            @Html.ValidationMessageFor(m => m.RegistrationData.Email)
        </div>
<div class="editor-label">
            @Html.LabelFor(m => m.RegistrationData.Password)
        </div>
        <div class="editor-field">
            @Html.PasswordFor(m => m.RegistrationData.Password)
            @Html.ValidationMessageFor(m => m.RegistrationData.Password)
        </div>
        <div class="editor-label">
            @Html.LabelFor(m => m.RegistrationData.ConfirmPassword)
        </div>
        <div class="editor-field">
            @Html.PasswordFor(m => m.RegistrationData.ConfirmPassword)
            @Html.ValidationMessageFor(m => m.RegistrationData.ConfirmPassword)
        </div>

Upvotes: 0

Views: 989

Answers (1)

Shyju
Shyju

Reputation: 218892

Add this Regular expression to Email Property and it will take care of it.

  [Required]
  [DataType(DataType.EmailAddress)]
  [Display(Name = "Email address")]
  [RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9]+(\\.[a-z]{2,4})$", ErrorMessage = "Not a valid email")]
  public string Email { get; set; }

Add a Required Attribute in front of the Confirm password field.

  [Required]
  [DataType(DataType.Password)]
  [Display(Name = "Confirm password")]
  [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
  public string ConfirmPassword { get; set; }

Scottgu's example to handle this. http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

Upvotes: 2

Related Questions