2

问题摘要: 当为特定模型解析流利的验证器时,是否可以从模型状态中清除错误列表?这样流式验证会覆盖数据注释模型提供程序中的默认行为,而不是对其进行补充?

我正在使用这样的流利验证:

FluentValidationModelValidatorProvider.Configure(
    _ =>
        {
            // This does not seem to work, or i am misunderstanding it?
            _.AddImplicitRequiredValidator = false;
        });

我将 autofac 用于容器,但流利的验证实际上还没有使用容器。我已经配置如上。

我有一个这样的模型:

[Validator(typeof(PartyModelValidator))]
public class PartyModel
{

验证器像这样验证...

public class PartyModelValidator : AbstractValidator<PartyModel>
{
    /// <summary>
    /// Initialises a new instance of the <see cref="PartyModelValidator"/> class.
    /// </summary>
    public PartyModelValidator()
    {
        this.RuleFor(_ => _.Client)
            .SetValidator(new ClientValidator())
            .When(_ => _.SelectedPartyTab == PartyType.Person);
        this.RuleFor(_ => _.Organisation)
            .SetValidator(new OrganisationValidator())
            .When(_ => _.SelectedPartyTab == PartyType.Organisation);

验证工作正常,除了[Required]一些人员和组织对象的属性。

这些[Required]属性显示为验证错误。即使整个对象实际上为空,我也会收到这些错误。

如上所述配置特定流式验证器时,如何获得流式验证以忽略数据注释属性?我宁愿将注释留在对象上,因为它们除了视图验证之外还有其他用途?

4

1 回答 1

1

这可以解决问题,以防其他人需要代码,就在这里。

/// <summary>
/// The fluent validation model validator provider ex.
/// </summary>
internal class FluentValidationModelValidatorProviderEx : FluentValidationModelValidatorProvider
{
    /// <summary>
    /// Initialises a new instance of the <see cref="FluentValidationModelValidatorProviderEx"/> class.
    /// </summary>
    /// <param name="validatorFactory">
    /// The validator factory.
    /// </param>
    public FluentValidationModelValidatorProviderEx(IValidatorFactory validatorFactory) : base(validatorFactory)
    {
    }

    /// <summary>
    /// get the fluent validators.
    /// </summary>
    /// <param name="metadata">The metadata.</param>
    /// <param name="context">The context.</param>
    /// <returns>the set of validators, if any validators are resolved.</returns>
    /// <remarks>If the fluent validator(s) are supplied, then clear the current set of model errors.</remarks>
    public override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context)
    {
        var validators = base.GetValidators(metadata, context);
        var modelValidators = validators as ModelValidator[] ?? validators.ToArray();
        if (modelValidators.Any())
            context.Controller.ViewData.ModelState.Clear();
        return modelValidators;
    }

    /// <summary>
    /// configure fluent validation.
    /// </summary>
    /// <param name="configurationExpression">The configuration expression.</param>
    internal static void ConfigureFluentValidation(Action<FluentValidationModelValidatorProvider> configurationExpression = null)
    {
        configurationExpression = configurationExpression ?? (Action<FluentValidationModelValidatorProvider>)(param0 => { });
        FluentValidationModelValidatorProvider validatorProvider = new FluentValidationModelValidatorProviderEx((IValidatorFactory)null);
        configurationExpression(validatorProvider);
        DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
        ModelValidatorProviders.Providers.Add((ModelValidatorProvider)validatorProvider);
    }
}
于 2013-09-29T06:30:26.823 回答