无法从 MVC2 中的自定义验证属性设置成员名称

发布于 2024-10-04 22:37:24 字数 1379 浏览 7 评论 0原文

我通过子类化 ValidationAttribute 创建了一个自定义验证属性。该属性在类级别应用于我的视图模型,因为它需要验证多个属性。

我正在覆盖

protected override ValidationResult IsValid(object value, ValidationContext validationContext)

并返回:

new ValidationResult("Always Fail", new List<string> { "DateOfBirth" }); 

在 DateOfBirth 是我的视图模型上的属性之一的所有情况下。

当我运行我的应用程序时,我可以看到它受到了影响。 ModelState.IsValid 正确设置为 false,但是当我检查 ModelState 内容时,我发现属性 DateOfBirth 不包含任何错误。相反,我有一个值为 null 的空字符串 Key 和一个包含我在验证属性中指定的字符串的异常。

这会导致使用 ValidationMessageFor 时我的 UI 中不会显示错误消息。如果我使用 ValidationSummary,那么我可以看到错误。这是因为它与属性无关。

看起来好像它忽略了我在验证结果中指定了成员名称的事实。

这是为什么?我该如何解决?

根据要求提供示例代码:

 [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public class ExampleValidationAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            // note that I will be doing complex validation of multiple properties when complete so this is why it is a class level attribute
            return new ValidationResult("Always Fail", new List<string> { "DateOfBirth" });
        }
    }

    [ExampleValidation]
    public class ExampleViewModel
    {
        public string DateOfBirth { get; set; }
    }

I have created a custom validation attribute by subclassing ValidationAttribute. The attribute is applied to my viewmodel at the class level as it needs to validate more than one property.

I am overriding

protected override ValidationResult IsValid(object value, ValidationContext validationContext)

and returning:

new ValidationResult("Always Fail", new List<string> { "DateOfBirth" }); 

in all cases where DateOfBirth is one of the properties on my view model.

When I run my application, I can see this getting hit. ModelState.IsValid is set to false correctly but when I inspect the ModelState contents, I see that the Property DateOfBirth does NOT contain any errors. Instead I have an empty string Key with a value of null and an exception containing the string I specified in my validation attribute.

This results in no error message being displayed in my UI when using ValidationMessageFor. If I use ValidationSummary, then I can see the error. This is because it is not associated with a property.

It looks as though it is ignoring the fact that I have specified the membername in the validation result.

Why is this and how do I fix it?

EXAMPLE CODE AS REQUESTED:

 [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public class ExampleValidationAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            // note that I will be doing complex validation of multiple properties when complete so this is why it is a class level attribute
            return new ValidationResult("Always Fail", new List<string> { "DateOfBirth" });
        }
    }

    [ExampleValidation]
    public class ExampleViewModel
    {
        public string DateOfBirth { get; set; }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风吹雪碎 2024-10-11 22:37:25

您需要设置 ErrorMessage 属性,例如:

 public class DOBValidAttribute : ValidationAttribute
{
    private static string _errorMessage = "Date of birth is a required field.";

    public DOBValidAttribute() : base(_errorMessage)
    {

    }
//etc......overriding IsValid....

You need to set the ErrorMessage property, so for example:

 public class DOBValidAttribute : ValidationAttribute
{
    private static string _errorMessage = "Date of birth is a required field.";

    public DOBValidAttribute() : base(_errorMessage)
    {

    }
//etc......overriding IsValid....
一紙繁鸢 2024-10-11 22:37:24

大家好。

还在寻找解决方案吗?

我今天已经解决了同样的问题。您必须创建自定义验证属性来验证 2 个日期(下面的示例)。然后您需要适配器(验证器),它将使用您的自定义属性验证模型。最后一件事是将适配器与属性绑定。也许一些例子会比我更好地解释它:)

我们开始吧:

DateCompareAttribute.cs:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class DateCompareAttribute : ValidationAttribute
{
    public enum Operations
    {
        Equals,            
        LesserThan,
        GreaterThan,
        LesserOrEquals,
        GreaterOrEquals,
        NotEquals
    };

    private string _From;
    private string _To;
    private PropertyInfo _FromPropertyInfo;
    private PropertyInfo _ToPropertyInfo;
    private Operations _Operation;

    public string MemberName
    {
        get
        {
            return _From;
        }
    }

    public DateCompareAttribute(string from, string to, Operations operation)
    {
        _From = from;
        _To = to;
        _Operation = operation;

        //gets the error message for the operation from resource file
        ErrorMessageResourceName = "DateCompare" + operation.ToString();
        ErrorMessageResourceType = typeof(ValidationStrings);
    }

    public override bool IsValid(object value)
    {
        Type type = value.GetType();

        _FromPropertyInfo = type.GetProperty(_From);
        _ToPropertyInfo = type.GetProperty(_To);

        //gets the values of 2 dates from model (using reflection)
        DateTime? from = (DateTime?)_FromPropertyInfo.GetValue(value, null);
        DateTime? to = (DateTime?)_ToPropertyInfo.GetValue(value, null);

        //compare dates
        if ((from != null) && (to != null))
        {
            int result = from.Value.CompareTo(to.Value);

            switch (_Operation)
            {
                case Operations.LesserThan:
                    return result == -1;
                case Operations.LesserOrEquals:
                    return result <= 0;
                case Operations.Equals:
                    return result == 0;
                case Operations.NotEquals:
                    return result != 0;
                case Operations.GreaterOrEquals:
                    return result >= 0;
                case Operations.GreaterThan:
                    return result == 1;
            }
        }

        return true;
    }

    public override string FormatErrorMessage(string name)
    {
        DisplayNameAttribute aFrom = (DisplayNameAttribute)_FromPropertyInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault();
        DisplayNameAttribute aTo = (DisplayNameAttribute)_ToPropertyInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault();

        return string.Format(ErrorMessageString,
            !string.IsNullOrWhiteSpace(aFrom.DisplayName) ? aFrom.DisplayName : _From,
            !string.IsNullOrWhiteSpace(aTo.DisplayName) ? aTo.DisplayName : _To);
    }
}

DateCompareAttributeAdapter.cs:

public class DateCompareAttributeAdapter : DataAnnotationsModelValidator<DateCompareAttribute> 
{
    public DateCompareAttributeAdapter(ModelMetadata metadata, ControllerContext context, DateCompareAttribute attribute)
        : base(metadata, context, attribute) {
    }

    public override IEnumerable<ModelValidationResult> Validate(object container)
    {
        if (!Attribute.IsValid(Metadata.Model))
        {
            yield return new ModelValidationResult
            {
                Message = ErrorMessage,
                MemberName = Attribute.MemberName
            };
        }
    }
}

Global.asax:

protected void Application_Start()
{
    // ...
    DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(DateCompareAttribute), typeof(DateCompareAttributeAdapter));
}

CustomViewModel.cs:

[DateCompare("StartDateTime", "EndDateTime", DateCompareAttribute.Operations.LesserOrEquals)]
public class CustomViewModel
{
    // Properties...

    public DateTime? StartDateTime
    {
        get;
        set;
    }

    public DateTime? EndDateTime
    {
        get;
        set;
    }
}

hello everybody.

Still looking for solution?

I've solved the same problem today. You have to create custom validation attribute which will validate 2 dates (example below). Then you need Adapter (validator) which will validate model with your custom attribute. And the last thing is binding adapter with attribute. Maybe some example will explain it better than me :)

Here we go:

DateCompareAttribute.cs:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class DateCompareAttribute : ValidationAttribute
{
    public enum Operations
    {
        Equals,            
        LesserThan,
        GreaterThan,
        LesserOrEquals,
        GreaterOrEquals,
        NotEquals
    };

    private string _From;
    private string _To;
    private PropertyInfo _FromPropertyInfo;
    private PropertyInfo _ToPropertyInfo;
    private Operations _Operation;

    public string MemberName
    {
        get
        {
            return _From;
        }
    }

    public DateCompareAttribute(string from, string to, Operations operation)
    {
        _From = from;
        _To = to;
        _Operation = operation;

        //gets the error message for the operation from resource file
        ErrorMessageResourceName = "DateCompare" + operation.ToString();
        ErrorMessageResourceType = typeof(ValidationStrings);
    }

    public override bool IsValid(object value)
    {
        Type type = value.GetType();

        _FromPropertyInfo = type.GetProperty(_From);
        _ToPropertyInfo = type.GetProperty(_To);

        //gets the values of 2 dates from model (using reflection)
        DateTime? from = (DateTime?)_FromPropertyInfo.GetValue(value, null);
        DateTime? to = (DateTime?)_ToPropertyInfo.GetValue(value, null);

        //compare dates
        if ((from != null) && (to != null))
        {
            int result = from.Value.CompareTo(to.Value);

            switch (_Operation)
            {
                case Operations.LesserThan:
                    return result == -1;
                case Operations.LesserOrEquals:
                    return result <= 0;
                case Operations.Equals:
                    return result == 0;
                case Operations.NotEquals:
                    return result != 0;
                case Operations.GreaterOrEquals:
                    return result >= 0;
                case Operations.GreaterThan:
                    return result == 1;
            }
        }

        return true;
    }

    public override string FormatErrorMessage(string name)
    {
        DisplayNameAttribute aFrom = (DisplayNameAttribute)_FromPropertyInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault();
        DisplayNameAttribute aTo = (DisplayNameAttribute)_ToPropertyInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault();

        return string.Format(ErrorMessageString,
            !string.IsNullOrWhiteSpace(aFrom.DisplayName) ? aFrom.DisplayName : _From,
            !string.IsNullOrWhiteSpace(aTo.DisplayName) ? aTo.DisplayName : _To);
    }
}

DateCompareAttributeAdapter.cs:

public class DateCompareAttributeAdapter : DataAnnotationsModelValidator<DateCompareAttribute> 
{
    public DateCompareAttributeAdapter(ModelMetadata metadata, ControllerContext context, DateCompareAttribute attribute)
        : base(metadata, context, attribute) {
    }

    public override IEnumerable<ModelValidationResult> Validate(object container)
    {
        if (!Attribute.IsValid(Metadata.Model))
        {
            yield return new ModelValidationResult
            {
                Message = ErrorMessage,
                MemberName = Attribute.MemberName
            };
        }
    }
}

Global.asax:

protected void Application_Start()
{
    // ...
    DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(DateCompareAttribute), typeof(DateCompareAttributeAdapter));
}

CustomViewModel.cs:

[DateCompare("StartDateTime", "EndDateTime", DateCompareAttribute.Operations.LesserOrEquals)]
public class CustomViewModel
{
    // Properties...

    public DateTime? StartDateTime
    {
        get;
        set;
    }

    public DateTime? EndDateTime
    {
        get;
        set;
    }
}
ㄟ。诗瑗 2024-10-11 22:37:24

我不知道有什么简单的方法可以解决此行为。这就是我讨厌数据注释的原因之一。使用 FluentValidation 进行同样的操作将是小菜一碟:

public class ExampleViewModelValidator: AbstractValidator<ExampleViewModel>
{
    public ExampleViewModelValidator()
    {
        RuleFor(x => x.EndDate)
            .GreaterThan(x => x.StartDate)
            .WithMessage("end date must be after start date");
    }
}

FluentValidation 具有出色的与 ASP.NET MVC 的支持和集成

I am not aware of an easy way fix this behavior. That's one of the reasons why I hate data annotations. Doing the same with FluentValidation would be a peace of cake:

public class ExampleViewModelValidator: AbstractValidator<ExampleViewModel>
{
    public ExampleViewModelValidator()
    {
        RuleFor(x => x.EndDate)
            .GreaterThan(x => x.StartDate)
            .WithMessage("end date must be after start date");
    }
}

FluentValidation has great support and integration with ASP.NET MVC.

锦欢 2024-10-11 22:37:24

返回验证结果时使用两个参数的构造函数。
向其传递一个数组,其中 context.MemberName 作为唯一值。
希望这有帮助

<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field, AllowMultiple:=False)>


Public Class NonNegativeAttribute
Inherits ValidationAttribute
Public Sub New()


End Sub
Protected Overrides Function IsValid(num As Object, context As ValidationContext) As ValidationResult
    Dim t = num.GetType()
    If (t.IsValueType AndAlso Not t.IsAssignableFrom(GetType(String))) Then

        If ((num >= 0)) Then
            Return ValidationResult.Success
        End If
        Return New ValidationResult(context.MemberName & " must be a positive number",     New String() {context.MemberName})

    End If

    Throw New ValidationException(t.FullName + " is not a valid type. Must be a number")
End Function

End Class

When returning the validation result use the two parameter constructor.
Pass it an array with the context.MemberName as the only value.
Hope this helps

<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field, AllowMultiple:=False)>


Public Class NonNegativeAttribute
Inherits ValidationAttribute
Public Sub New()


End Sub
Protected Overrides Function IsValid(num As Object, context As ValidationContext) As ValidationResult
    Dim t = num.GetType()
    If (t.IsValueType AndAlso Not t.IsAssignableFrom(GetType(String))) Then

        If ((num >= 0)) Then
            Return ValidationResult.Success
        End If
        Return New ValidationResult(context.MemberName & " must be a positive number",     New String() {context.MemberName})

    End If

    Throw New ValidationException(t.FullName + " is not a valid type. Must be a number")
End Function

End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文