If you want to handle an empty case, you will need a zero integer on your model, because otherwise it is the default connecting device that will automatically add a validation error when it tries to parse an empty string into a non-nullable integer:
public int? MaxNumberTeamMembers { get; set; }
and then you can have the following rules for checking this property:
RuleFor(x => x.MaxNumberTeamMembers) .NotEmpty() .WithMessage("Max. number of team members is required") .Must(x => x.Value > 0) .When(x => x.MaxNumberTeamMembers != null) .WithMessage("Max. number of team members must be greater than 0");
Strike>
UPDATE:
The following works fine with the latest version of FluentValidation:
RuleFor(x => x.MaxNumberTeamMembers) .NotNull() .WithMessage("Max. number of team members is required") .GreaterThan(0) .WithMessage("Max. number of team members must be greater than 0");
source share