Property-level validation errors hinder the validation of Class-level validation

后端 未结 3 2183
难免孤独
难免孤独 2020-12-18 05:15

Update after Bounty was awarded

A new solution is coming up to this problem. Please refer to ASP.NET MVC 3 Preview 1 here: http://weblogs.asp.net/sc

3条回答
  •  醉梦人生
    2020-12-18 05:48

    To expand on the link to Scott Guthrie's article, starting with MVC 3, you can perform class level validation by implementing the IValidatableObject Interface.

    This will work inside the context of the current validation pipeline so it can be nested on as many custom classes and properties as you like and continue to return the full array of possible error messages.

    For your class, ditch the class level attributes, and add a method called Validate like this:

    public class EditSiteUser : IValidatableObject
    {
        public int UserId { get; set; }
    
        [Required(ErrorMessage = "Du skal indtaste et brugernavn")]
        public string Username { get; set; }
    
        [Required(ErrorMessage = "Du skal indtaste en adgangskode")]
        public string Password { get; set; }
    
        [Required(ErrorMessage="Du skal bekræfte adgangskode")]
        public string PasswordConfirm { get; set; }
    
        [Required(ErrorMessage = "Du skal indtaste en e-mailadresse")]
        [Email(ErrorMessage = "Ugyldig e-mailadresse")]
        public string Email { get; set; }
    
        [Required(ErrorMessage="Du skal bekræfte e-mailadressen")]
        [Email(ErrorMessage="Ugyldig e-mailadresse")]
        public string EmailConfirm { get; set; }
    
    
        public IEnumerable Validate(ValidationContext validationContext)
        { 
          // put whatever class level validation you want here
    
          if (Email !== EmailConfirm)
          {
              yield return new ValidationResult("E-mailadresserne skal være ens", new[] {"EmailConfirm"})
          }
    
          if (Password !== PasswordConfirm)
          {
              yield return new ValidationResult("Adgangskoderne skal være ens", new[] {"PasswordConfirm"})
          }
        }
    
    }
    

    You can continue to yield return as many validation messages as you'd like.

    And you can display them all on the client with @Html.ValidationSummary

    If you'd like the message to appear alongside a particular control, the ValidationResult constructor takes an overload with the memberNames of the affected properties, and you can provide the validation message for that particular property with the ValidationMessageFor HTML helper like this:

    @Html.ValidationMessageFor(Function(model) model.TestOne )
    

    Also, it's worth mentioning that you can use the CompareValidator to easily ensure the values of two different properties are equal. The upside to using this annotation is that it automatically knows how to enforce this on the client as well as the server, whereas adding IValidatableObject will only run on the server.

    [DataType(DataType.Password)]
    public string Password { get; set; }
    
    [Compare("Password")]
    [DataType(DataType.Password)]
    public string ConfirmPassword { get; set; }
    

    For further reading, Scott Guthrie another post with more detail on class level validation.

提交回复
热议问题