I tried FluentValidation in a project that contains complex view models, and I read it here , but I donβt see how to set up rules to check the list of objects declared in my view model. In my example below, the list in the view model contains 1 or more guitar objects. Thanks
Show model
[FluentValidation.Attributes.Validator(typeof(CustomerViewModelValidator))] public class CustomerViewModel { [Display(Name = "First Name")] public string FirstName { get; set; } [Display(Name = "Last Name")] public string LastName { get; set; } [Display(Name = "Phone")] public string Phone { get; set; } [Display(Name = "Email")] public string EmailAddress { get; set; } public List<Guitar> Guitars { get; set; } }
The guitar class used in the view model.
public class Guitar { public string Make { get; set; } public string Model { get; set; } public int? ProductionYear { get; set; } }
Show model validation class
public class CustomerViewModelValidator : AbstractValidator<CustomerViewModel> { public CustomerViewModelValidator() { RuleFor(x => x.FirstName).NotNull(); RuleFor(x => x.LastName).NotNull(); RuleFor(x => x.Phone).NotNull(); RuleFor(x => x.EmailAddress).NotNull();
c # asp.net-mvc-3 fluentvalidation
Slinky
source share