我有以下viewmodel定义
public class AccessRequestViewModel
{
    public Request Request { get; private set; }
    public SelectList Buildings { get; private set; }
    public List<Person> Persons { get; private set; }
}
所以在我的应用程序中,访问请求必须至少有一个人.您可以使用什么方法进行验证?我不希望在我的控制器中进行这种验证,这很简单.是自定义验证属性的唯一选择吗?
编辑:目前正在使用FluentValidation执行此验证(漂亮的库!)
RuleFor(vm => vm.Persons)
                .Must((vm, person) => person.Count > 0)
                .WithMessage("At least one person is required");
我需要的是一种根据是否填写其他字段来有条件地验证字段的方法.
防爆.我有一个相关的下拉列表和日期字段.如果没有设置任何字段,则表单应通过验证.但是,如果设置了两个字段中的一个但另一个未设置则应激活验证,则需要设置其他字段.
我编写了自定义验证类,但似乎它在单个字段上有效.有没有办法使用内置验证器设置我需要的验证?如果没有,是否有一种使用自定义验证器连接两个字段的好方法?
我有一个数据类层次结构
public class Base
{
    // Fields to be validated
}
public class Derived1 : Base
{
    // More fields to be validated
}
public class Derived2 : Base
{
    // More fields to be validated
}
使用FluentValidation框架验证Derived1和Derived2的适当方法是什么,而不重复Base类字段的规则?
我有以下规则
第一个使用不显眼的客户端验证工作,第二个没有
任何想法为什么?
RuleFor(x => x.StartDate)
    .LessThanOrEqualTo(x => x.EndDate.Value)
    .WithLocalizedMessage(() => CommonRes.Less_Than_Or_Equal_To, filters => CommonRes.Start_Date, filters => CommonRes.End_Date);
RuleFor(x => x.StartDate)
    .GreaterThanOrEqualTo(x => x.AbsoluteStartDate)
    .LessThanOrEqualTo(x => x.AbsoluteEndDate)
    .WithLocalizedMessage(() => CommonRes.Between, filters => CommonRes.Start_Date, filters => filters.AbsoluteStartDate, filters => filters.AbsoluteEndDate);
我有这个模型
public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }  
}
我想创建一个验证,其中FirstName或LastName必须由用户填写.我安装FluentValidation并创建了一个customvalidator类
public class PersonValidator:AbstractValidator<Person>
{
    public PersonValidator()
    {
        RuleFor((person=>person.FirstName)//don't know how to check if one is empty
    }
}
要检查我可以做的一个字段 RuleFor(person => person.FirstName).NotNull();
但是我如何检查其中一个是否为空.
此外,一旦通过验证创建,是否有可能fluentValidation在客户端使用它来显示错误?
EDIT1
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        FluentValidationModelValidatorProvider.Configure();
    }
//creating validation
namespace WebApplication1.Models.CustomValidator
{
    public class PersonValidator:AbstractValidator<Person>
    {
        public PersonValidator() …我一直在尝试创建一个FluentValidation规则,在验证它的属性之前检查它验证的对象的实例是否为null.
我宁愿在验证器中封装这个空验证,而不是在调用代码中执行它.
请参阅下面的示例代码以及需要所需逻辑的注释:
namespace MyNamespace
{
    using FluentValidation;
    public class Customer
    {
        public string Surname { get; set; }
    }
    public class CustomerValidator: AbstractValidator<Customer> 
    {
        public CustomerValidator() 
        {
            // Rule to check the customer instance is not null.
            // Don't continue validating.
            RuleFor(c => c.Surname).NotEmpty();
        }
    }
    public class MyClass
    {
        public void DoCustomerWork(int id)
        {
            var customer = GetCustomer(id);
            var validator = new CustomerValidator();
            var results = validator.Validate(customer);
            var validationSucceeded = results.IsValid;
        }
        public Customer GetCustomer(int id)
        {
            return …我有一个FluentValidator,它有多个属性,如zip和county等.我想创建一个规则,它接受两个属性,就像RuleFor构造一样
public class FooArgs
{
    public string Zip { get; set; }
    public System.Guid CountyId { get; set; }
}
public class FooValidator : AbstractValidator<FooArgs>
{
    RuleFor(m => m.CountyId).Must(ValidZipCounty).WithMessage("wrong Zip County");
}
这有效,但我想将Zip和县都传递到rue以便验证.实现这一目标的最佳方法是什么?
有一个表单,用户可以在其中输入事件的开始日期/时间和结束日期/时间.到目前为止,这是验证器:
public class EventModelValidator : AbstractValidator<EventViewModel>
    {
        public EventModelValidator()
        {
            RuleFor(x => x.StartDate)
                .NotEmpty().WithMessage("Date is required!")
                .Must(BeAValidDate).WithMessage("Invalid date");
            RuleFor(x => x.StartTime)
                .NotEmpty().WithMessage("Start time is required!")
                .Must(BeAValidTime).WithMessage("Invalid Start time");
            RuleFor(x => x.EndTime)
                .NotEmpty().WithMessage("End time is required!")
                .Must(BeAValidTime).WithMessage("Invalid End time");
            RuleFor(x => x.Title).NotEmpty().WithMessage("A title is required!");
        }
        private bool BeAValidDate(string value)
        {
            DateTime date;
            return DateTime.TryParse(value, out date);
        }
        private bool BeAValidTime(string value)
        {
            DateTimeOffset offset;
            return DateTimeOffset.TryParse(value, out offset);
        }
    }
现在我还想添加EndDateTime> StartDateTime(组合日期+时间属性)的验证,但不知道如何去做.
编辑: 为了澄清,我需要以某种方式结合EndDate + EndTime/StartDate + StartTime即DateTime.Parse(src.StartDate +""+ src.StartTime),然后验证EndDateTime与StartDateTime …
我正在尝试将Fluent验证连接到我的MVC WEB Api项目,它不想工作.
当我使用MyController : Controller- >工作正常(ModelState.IsValid返回False)
但是当我使用MyController :ApiController......没什么.
有没有人有关于如何挂钩的经验?