質問

Ninject、MVC4、AutomApper、FluentValidationを使用して接着しています。

私は自分のビューモデルのバリデーターを作成しましたが、ビューモデルバリーター内で呼び出されなければならない再利用可能なバリデーターを書きました。

問題は、フォームを投稿すると、検証型オーバーライドがビューモデルバリデーターで呼び出されないため、再利用可能なバリデーターも呼び出されないため、最終的にはモデルのresultが有効です... db)...

奇妙なことは、プロパティの1つに対してルールを追加したとき、フォームがうまく検証されているということです。

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

ここで何が間違っているのでしょうか?

役に立ちましたか?

解決

あなたは間違った過負荷を無効にしています。署名を使用して検証メソッドをオーバーライドする必要があります。 public virtual ValidationResult Validate(ValidationContext<T> 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);
 }

他の過負荷は、次のようにVALIDATEを手動で呼び出す場合にのみ使用されます validator.Validate(object).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top