Question

I am trying out FluentValidation on a project that contains complex view models and I read the documentation here but I don't see how to set up the rules to validate a 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

View 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; } 
    }

Guitar class used in View Model

public class Guitar
{
    public string Make { get; set; }
    public string Model { get; set; }
    public int? ProductionYear { get; set; }
}

View Model Validator 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();
           //Expects an indexed list of Guitars here????


        }
    }
Was it helpful?

Solution 2

You would add this to your CustomerViewModelValidator

RuleFor(x => x.Guitars).SetCollectionValidator(new GuitarValidator());

So your CustomerViewModelValidator would look like this:

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();
        RuleFor(x => x.Guitars).SetCollectionValidator(new GuitarValidator());
    }
}

Add the GuitarValidator would look something like:

public class GuitarValidator : AbstractValidator<Guitar>
{
    public GuitarValidator()
    {
        // All your other validation rules for Guitar. eg.
        RuleFor(x => x.Make).NotNull();
    }
 }

OTHER TIPS

This code deprecated: RuleFor(x => x.Guitars).SetCollectionValidator(new GuitarValidator());

This is new:

RuleForEach(x => x.Guitars).SetValidator(new GuitarValidator());
RuleForEach(
  itemToValidate => 
     new YourObjectValidator());


public class YourObjectValidator : AbstractValidator<YourObject>
{
  public EdgeAPIAddressValidator()
  {
     RuleFor(r => r.YourProperty)
         .MaximumLenght(100);
  }
}

It works with the latest version of Fluent and contains a complete example to be used.

The code in the answer it's deprecated.

You can use childrule

RuleForEach(p => p.Guitars).ChildRules(child =>{
child.RuleFor(x=>x.Make).NotEmpty().WithMessage({PropertyName}required.") .NotNull(); });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top