Sco*_*ott 38 c# validation asp.net-mvc data-annotations
我如何编写一个比较两个字段的自定义ValidationAttribute?这是常见的"输入密码","确认密码"方案.我需要确保两个字段相同并保持一致,我想通过DataAnnotations实现验证.
所以在伪代码中,我正在寻找一种方法来实现如下所示:
public class SignUpModel
{
[Required]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[Display(Name = "Re-type Password")]
[Compare(CompareField = Password, ErrorMessage = "Passwords do not match")]
public string PasswordConfirm { get; set; }
}
public class CompareAttribute : ValidationAttribute
{
public CompareAttribute(object propertyToCompare)
{
// ??
}
public override bool IsValid(object value)
{
// ??
}
}
Run Code Online (Sandbox Code Playgroud)
所以问题是,我如何编码[Compare] ValidationAttribute?
Jan*_*ela 58
确保您的项目引用system.web.mvc v3.xxxxx.
然后你的代码应该是这样的:
using System.Web.Mvc;
Run Code Online (Sandbox Code Playgroud)
....
[Required(ErrorMessage = "This field is required.")]
public string NewPassword { get; set; }
[Required(ErrorMessage = "This field is required.")]
[Compare(nameof(NewPassword), ErrorMessage = "Passwords don't match.")]
public string RepeatPassword { get; set; }
Run Code Online (Sandbox Code Playgroud)
Joe*_*ano 30
ASP.NET MVC 3 Framework中有一个CompareAttribute来执行此操作.如果您正在使用ASP.NET MVC 2并以.Net 4.0为目标,那么您可以查看ASP.NET MVC 3源代码中的实现.
这是Darin答案的较长版本:
public class CustomAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
if (value.GetType() == typeof(Foo))
{
Foo bar = (Foo)value;
//compare the properties and return the result
}
throw new InvalidOperationException("This attribute is only valid for Foo objects");
}
}
Run Code Online (Sandbox Code Playgroud)
和用法:
[MetadataType(typeof(FooMD))]
public partial class Foo
{
... functions ...
}
[Custom]
public class FooMD
{
... other data annotations ...
}
Run Code Online (Sandbox Code Playgroud)
错误将显示在 @Html.ValidationSummary(false)
| 归档时间: |
|
| 查看次数: |
60093 次 |
| 最近记录: |