【问题标题】:Validate not called in custom validator验证未在自定义验证器中调用
【发布时间】:2013-11-08 04:57:45
【问题描述】:

我将 Ninject、MVC4、AutoMapper 和 FluentValidation 结合使用。

我已经为我的视图模型编写了一个验证器,并且我已经编写了一个必须在视图模型验证器中调用的可重用验证器。

问题是,当我发布表单时,没有在视图模型验证器上调用 Validate 覆盖,所以也没有调用可重用验证器,所以最后 ModelResult 是有效的......(导致异常时将实体写入数据库)...

奇怪的是,当我为其中一个属性添加 RuleFor 时,表单得到了很好的验证。

public class RequiredSourceViewModelValidator : AbstractValidator<RequiredSourceViewModel>
    {
        public RequiredSourceViewModelValidator()
        {
            Mapper.CreateMap<RequiredSourceViewModel, Source>();
        }

        public override FluentValidation.Results.ValidationResult Validate(RequiredSourceViewModel requiredSourceViewModel)
        {
            var validator = new SourceValidator();

            var source = Mapper.Map<RequiredSourceViewModel, Source>(requiredSourceViewModel);

            return validator.Validate(source);
        }
    }


public class SourceValidator : AbstractValidator<Source>
    {
        public SourceValidator()
        {
            RuleFor(s => s.Name)
                .NotEmpty()
                    .WithMessage("Naam mag niet leeg zijn.")
                .Length(1, 100)
                    .WithMessage("Naam mag niet langer zijn dan 100 karakters.");

            RuleFor(s => s.Url)
                .NotEmpty()
                    .WithMessage("Url mag niet leeg zijn.")
                 .Must(BeAValidUrl)
                    .WithMessage("Url is niet geldig.")
                .Length(1, 100)
                    .WithMessage("Url mag niet langer zijn dan 100 karakters.");
        }

        private bool BeAValidUrl(string url)
        {
            if (url == null)
            {
                return true;
            }

            var regex = new Regex(@"^http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$");
            return regex.IsMatch(url);
        }
    }

public class Source : IEntity
    {
        /// <summary>
        /// Gets or sets the primary key of the source.
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// Gets or sets the name of the source.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the url of the source.
        /// </summary>
        public string Url { get; set; }

        /// <summary>
        /// Gets or sets the ordinal of the source.
        /// </summary>
        /// <value>
        /// The ordinal of the source.
        /// </value>
        public int Ordinal { get; set; }

        public int? GameId { get; set; }
    }

这里有什么问题?

【问题讨论】:

    标签: c# asp.net-mvc fluentvalidation


    【解决方案1】:

    您正在覆盖错误的重载。您需要使用签名覆盖 Validate 方法:public virtual ValidationResult Validate(ValidationContext&lt;T&gt; context) 因为此方法将在 MVC 验证期间被调用:

    public override ValidationResult Validate(
          ValidationContext<RequiredSourceViewModel> context)
    {
         var validator = new SourceValidator();
    
         var source = 
             Mapper.Map<RequiredSourceViewModel, Source>(context.InstanceToValidate);
    
         return validator.Validate(source);
     }
    

    仅当您像validator.Validate(object) 一样手动调用验证时才使用其他重载。

    【讨论】:

      猜你喜欢
      • 2016-12-04
      • 2013-11-15
      • 2018-01-19
      • 2013-08-18
      • 2015-01-02
      • 1970-01-01
      • 2020-09-22
      • 2016-02-10
      • 1970-01-01
      相关资源
      最近更新 更多