Reputation: 4909
I have an MVC3 view model defined as:
[Validator(typeof(AccountsValidator))]
public class AccountViewModel
{
public List<string> Accounts { get; set; }
}
With the validation defined using FluentValidation (v3.3.1.0) as:
public class AccountsValidator : AbstractValidator<AccountViewModel>
{
public AccountsValidator()
{
RuleFor(x => x.Accounts).SetCollectionValidator(new AccountValidator()); //This won't work
}
}
And the account validation would possibly be defined:
public class AccountValidator : AbstractValidator<string> {
public OrderValidator() {
RuleFor(x => x).NotNull();
//any other validation here
}
}
I would like each account in the list to be valdiated as described in the documentation. However, the call to SetCollectionValidator
doesn't work as this is not an option when using a List<string>
although the option would be there if it were defined as List<Account>
. Am I missing something? I could change my model to use List<Account>
and then define an Account class but I don't really want to change my model to suit the validation.
For reference, this is the view that I am using:
@model MvcApplication9.Models.AccountViewModel
@using (Html.BeginForm())
{
@*The first account number is a required field.*@
<li>Account number* @Html.EditorFor(m => m.Accounts[0].Account) @Html.ValidationMessageFor(m => m.Accounts[0].Account)</li>
for (int i = 1; i < Model.Accounts.Count; i++)
{
<li>Account number @Html.EditorFor(m => m.Accounts[i].Account) @Html.ValidationMessageFor(m => m.Accounts[i].Account)</li>
}
<input type="submit" value="Add more..." name="add"/>
<input type="submit" value="Continue" name="next"/>
}
Upvotes: 19
Views: 30712
Reputation: 7592
You could use RuleForEach
There is an easy class with a list of string
public class Request
{
public IEnumerable<string> UserIds { get; set; }
public string Body { get; set; }
}
I created the next validator
public class RequestValidator : AbstractValidator<Request>
{
public RequestValidator()
{
RuleForEach(x => x.UserIds).NotNull().NotEmpty();
RuleFor(x => x.Body).NotNull().NotEmpty();
}
}
Upvotes: 1
Reputation: 3293
Try to use:
public class AccountsValidator : AbstractValidator<AccountViewModel>
{
public AccountsValidator()
{
RuleForEach(x => x.Accounts).NotNull()
}
}
Upvotes: 6
Reputation: 1056
Validation classes:
using FluentValidation;
using System.Collections.Generic;
namespace Test.Validator
{
public class EmailCollection
{
public IEnumerable<string> email { get; set; }
}
public class EmailValidator: AbstractValidator<string>
{
public EmailValidator()
{
RuleFor(x => x).Length(0, 5);
}
}
public class EmailListValidator: AbstractValidator<EmailCollection>
{
public EmailListValidator()
{
RuleFor(x => x.email).SetCollectionValidator(new EmailValidator());
}
}
}
Note: I used latest MVC 5 (Nuget) version of fluentvalidation.
Upvotes: 0
Reputation: 1038880
The following should work:
public class AccountsValidator : AbstractValidator<AccountViewModel>
{
public AccountsValidator()
{
RuleFor(x => x.Accounts).SetCollectionValidator(
new AccountValidator("Accounts")
);
}
}
public class AccountValidator : AbstractValidator<string>
{
public AccountValidator(string collectionName)
{
RuleFor(x => x)
.NotEmpty()
.OverridePropertyName(collectionName);
}
}
Upvotes: 23