属性级的验证错误阻碍了类级别的验证的验证 [英] Property-level validation errors hinder the validation of Class-level validation
问题描述
赏金后更新被授予
一个新的解决方案来了这个问题。请参见ASP.NET MVC 3 preVIEW 1浏览:<一href=\"http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-$p$pview-1.aspx\">http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-$p$pview-1.aspx
A new solution is coming up to this problem. Please refer to ASP.NET MVC 3 Preview 1 here: http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx
在外观部分的验证模型的改进,在那里你会看到解决我的问题。
Look in the section Validation Model improvements, where you will see the solution to my problem.
原贴
参照我先前的职位<一个href=\"http://stackoverflow.com/questions/2450198/how-to-validate-two-properties-with-asp-net-mvc-2\">How在ASP.NET MVC 2 ,我问我怎么能为模型验证比较两个属性来验证两个属性。
Referring to my earlier post How to validate two properties with ASP.NET MVC 2 where I asked how I could compare two properties for Model validation.
我没有找到答案有用的,但我留下了一个完全不同的问题:
I did find the answer useful, but I was left with an entirely different problem:
问题:如果房产级ValidationAttribute包含一个错误,那么类级别ValidationAttributes不验证
Problem: If a Property-level ValidationAttribute contains an error, then the Class-level ValidationAttributes are NOT validated.
请考虑以下几点:
[EqualTo("Email", "EmailConfirm", ErrorMessage = "E-mailadresserne skal være ens")]
[EqualTo("Password", "PasswordConfirm", ErrorMessage = "Adgangskoderne skal være ens")]
[Bind(Exclude="UserId")]
public class EditSiteUser
{
[Required(ErrorMessage="Du skal bekræfte adgangskode")]
public string PasswordConfirm { get; set; }
[Required(ErrorMessage="Du skal bekræfte e-mailadressen")]
[Email(ErrorMessage="Ugyldig e-mailadresse")]
public string EmailConfirm { get; set; }
public int UserId { get; set; }
[Required(ErrorMessage = "Du skal indtaste et brugernavn")]
public string Username { get; set; }
[Required(ErrorMessage = "Du skal indtaste en adgangskode")]
public string Password { get; set; }
[Required(ErrorMessage = "Du skal indtaste en e-mailadresse")]
[Email(ErrorMessage = "Ugyldig e-mailadresse")]
public string Email { get; set; }
}
在这里,我有一个验证EmailConfirm和PasswordConfirm两个类级别attibutes。
Here I have two Class-level attibutes that validate EmailConfirm and PasswordConfirm.
如果像用户名的字段为空,这样会产生错误,那么这两个属性EqualTo从不验证。
If a field like Username is empty, and thus yields an error, then the two EqualTo Attributes are never validated.
有没有人有一个建议,解决这个问题?
Does anyone have a suggestion to overcome this problem?
编辑:如果您需要了解这个问题了,请问意见,我会很乐意给你所需要的任何其他信息
If you need anymore information about this problem, please ask in comments and I will be very happy to give you any additional information you need.
问:为什么很重要,如果一个属性级检查失败的类级检查得到验证?
Q: "Why is it important that the class-level checks get validated if a property-level check fails?".
答:因为这是一个形式,即用户输入的信息成一种形式,通过帖子后面AJAX表单时返回它应该显示当前所有问题的一部分。
A: "Because this is part of a form, where a user enters information into a form that posts back via AJAX. When the form returns it should show all current problems."
问:????到底是什么EqualTo属性已放置在类它是一个自定义的验证属性,如果是这样,它是如何工作的?它有什么
Q: "What exactly is the EqualTo attribute you have placed on the class? Is it a custom validation attribute? If so, how does it work? what does it do?"
答:EqualTo是一类级别ValidationAttribute用于比较类实例的两个属性的值。查一查PropertiesMustMatchAttribute的一个类似的实现。
A: EqualTo is a class-level ValidationAttribute that compares the value of two Properties of the class-instance. Look up "PropertiesMustMatchAttribute" for a similar implementation.
推荐答案
这是不支持的。如果任何财产级验证失败,那么类级验证不执行。我建议你看一下 MVC万无一失验证的。它扩展了MVC验证添加的特遣队属性验证支持。我认为这将解决问题的这种特殊情况下。
This isn't supported. If any of the property level validations fail, then the class level validations are not performed. I suggest you look at MVC Foolproof Validation. It extends MVC validation to add support for contingent property validation. I think that would solve the problem for this particular case.
该项目选址指出,它不与MVC2 RC工作,所以你必须下载源$ C $ c和让它运行/采纳他们的想法自己。
The project site states that it doesn't work with the MVC2 RC, so you'll have to download the source code and get it running/adopt their ideas yourself.
这篇关于属性级的验证错误阻碍了类级别的验证的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!