指定的ValidationResult具体的领域呢? [英] Assign ValidationResult to specific field?

查看:165
本文介绍了指定的ValidationResult具体的领域呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用IValidatableObject相对于彼​​此验证表单值。我想一个为ValidationResult分配到特定的字段​​,所以我可以旁边显示领域的错误消息。

I am trying to use IValidatableObject to validate form values with respect to each other. I would like to assign a ValidationResult to a specific field so I can display an error message next to that field.

例如,我可能希望具有用户输入密码两次,并有验证失败,如果第2次是不等于所述第一,则有错误消息显示在第二旁。

For example, I may want to have users enter passwords twice, and have validation fail if the 2nd is not equal to the first, then have the error message display next to the second.

不过似乎我只能分配在模型级这样的错误;通过模型级我的意思是,将与 @ Html.ValidationSummary(真)显示一个错误,那就是不依赖于特定的模型字段。

However it seems I can only assign errors at the model-level this way; by model-level I mean an error that will display with @Html.ValidationSummary(true), and that isn't tied to a specific model field.

我检查的ValidationResult的声明,我没有看到,看起来这个帮助的任何属性或方法。所以 - ?任何人都可以或者告诉我一个方法,一个为ValidationResult从IValidateableObject的验证方法中分配给特定字段,或者确认它是不可能这样做的。

I've examined the declaration for ValidationResult, and I don't see any property or method that looks helpful for this. So -- can anyone either show me a way to assign a ValidationResult to a specific field from within a Validate method of an IValidateableObject, or confirm that it is NOT possible do so?

(注意:我不是在寻找一个解决方法请 - 没有对答案,例如,过滤器,是以密码例如照顾我只是想了解IValidateObject的极限,特别是知道的。)

(NOTE: I am NOT looking for a work-around. PLEASE -- no answers about, for example, filters that takes care of the password example. I only want to know specifically about the limits of IValidateObject.)

推荐答案

有可能,为ValidationResult具有接受字符串的IEnumarable过载,要关联的属性。

Is possible, ValidationResult has an overload that accept IEnumarable of string, properties to be associated.

一个例子。

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (Password != PasswordConfirmation)
            yield return new ValidationResult("Password confirmation must match", new[] { "PasswordConfirmation" }); 
    }

这篇关于指定的ValidationResult具体的领域呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆