ShaneKm
ShaneKm

Reputation: 21338

MVC Validating Strongly Typed Views

I have following ViewModel:

    public IEnumerable<SelectListItem> SelectAdminsInGroup { get; set; }
    public IEnumerable<SelectListItem> SelectAdminsNotInGroup { get; set; }
    public model.Admin Admin { get; set; }

Admin class:

[PropertiesMustMatchAttribute("Password","ConfirmPassword")] public class Admin { public Admin() { this.PasswordDate = DateTime.Now; this.Username = string.Empty; }

    public virtual int AdminId { get; set; }

    [Required(ErrorMessage = "Field 'Username' is required"), StringLength(20, ErrorMessage = "Field 'Username' must be less than 20 characters long")]
    public virtual string Username { get; set; }
    public virtual string Oldusername { get; set; }

    [ValidatePasswordLength()]
    public virtual string Password { get; set; }
    public virtual string ConfirmPassword { get; set; }


    public virtual ICollection<AdminGroup> AdminGroup { get; set; }
}

Now for some reason PropertiesMustMatch never displays the error message in the validation summary. i also noticed that one of the ModelState keys contains "Admin" for "PropertiesMustMatchAttribute" and it should be blank since this would be the class/summary error message. How would i validate my custom model? thanks

Upvotes: 0

Views: 285

Answers (1)

Jay
Jay

Reputation: 2703

Type-level validators (e.g. PropertiesMustMatchAttribute) run only if all of the property-level validators succeeded. If the property-level validator fails, the type-level validator will not be run. Are you failing the "ValidatePasswordLength" check?

Upvotes: 2

Related Questions