我有一个具有4个属性的模型,其类型为string。我知道您可以使用StringLength注释验证单个属性的长度。但是我想验证4个属性组合的长度。
使用数据注释执行此操作的MVC方法是什么?
我问这个是因为我是MVC的新手,并希望在制作自己的解决方案之前以正确的方式做到这一点。
答案 0 :(得分:165)
您可以编写自定义验证属性:
public class CombinedMinLengthAttribute: ValidationAttribute
{
public CombinedMinLengthAttribute(int minLength, params string[] propertyNames)
{
this.PropertyNames = propertyNames;
this.MinLength = minLength;
}
public string[] PropertyNames { get; private set; }
public int MinLength { get; private set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var properties = this.PropertyNames.Select(validationContext.ObjectType.GetProperty);
var values = properties.Select(p => p.GetValue(validationContext.ObjectInstance, null)).OfType<string>();
var totalLength = values.Sum(x => x.Length) + Convert.ToString(value).Length;
if (totalLength < this.MinLength)
{
return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
}
return null;
}
}
然后你可能有一个视图模型并用它来装饰它的一个属性:
public class MyViewModel
{
[CombinedMinLength(20, "Bar", "Baz", ErrorMessage = "The combined minimum length of the Foo, Bar and Baz properties should be longer than 20")]
public string Foo { get; set; }
public string Bar { get; set; }
public string Baz { get; set; }
}
答案 1 :(得分:86)
您的模型应实现接口IValidatableObject
。将验证码放在Validate
方法中:
public class MyModel : IValidatableObject
{
public string Title { get; set; }
public string Description { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Title == null)
yield return new ValidationResult("*", new [] { nameof(Title) });
if (Description == null)
yield return new ValidationResult("*", new [] { nameof(Description) });
}
}
请注意:这是服务器端验证。它不适用于客户端。您的验证将仅在表单提交后执行。
答案 2 :(得分:22)
ExpressiveAnnotations为您提供了这样的可能性:
[Required]
[AssertThat("Length(FieldA) + Length(FieldB) + Length(FieldC) + Length(FieldD) > 50")]
public string FieldA { get; set; }
答案 3 :(得分:9)
<强>背景强>
需要进行模型验证,以确保我们收到的接收数据有效且正确,以便我们可以使用此数据进行进一步处理。我们可以在动作方法中验证模型。内置的验证属性是Compare,Range,RegularExpression,Required,StringLength。但是,我们可能有一些场景,其中我们需要验证属性而不是内置属性。
自定义验证属性
public class EmployeeModel
{
[Required]
[UniqueEmailAddress]
public string EmailAddress {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public int OrganizationId {get;set;}
}
要创建自定义验证属性,您必须从ValidationAttribute派生此类。
public class UniqueEmailAddress : ValidationAttribute
{
private IEmployeeRepository _employeeRepository;
[Inject]
public IEmployeeRepository EmployeeRepository
{
get { return _employeeRepository; }
set
{
_employeeRepository = value;
}
}
protected override ValidationResult IsValid(object value,
ValidationContext validationContext)
{
var model = (EmployeeModel)validationContext.ObjectInstance;
if(model.Field1 == null){
return new ValidationResult("Field1 is null");
}
if(model.Field2 == null){
return new ValidationResult("Field2 is null");
}
if(model.Field3 == null){
return new ValidationResult("Field3 is null");
}
return ValidationResult.Success;
}
}
希望这会有所帮助。干杯!
<强>参考强>
答案 4 :(得分:8)
为了改善达林的答案,它可以缩短一点:
public class UniqueFileName : ValidationAttribute
{
private readonly NewsService _newsService = new NewsService();
public override bool IsValid(object value)
{
if (value == null) { return false; }
var file = (HttpPostedFile) value;
return _newsService.IsFileNameUnique(file.FileName);
}
}
型号:
[UniqueFileName(ErrorMessage = "This file name is not unique.")]
请注意,需要输入错误消息,否则错误将为空。
答案 5 :(得分:1)
回答有点迟,但对于谁在搜索。 您可以通过使用带有数据注释的额外属性轻松完成此操作:
public string foo { get; set; }
public string bar { get; set; }
[MinLength(20, ErrorMessage = "too short")]
public string foobar
{
get
{
return foo + bar;
}
}
这真的太棒了。如果您确实希望在特定位置显示验证错误,可以在视图中添加:
@Html.ValidationMessage("foobar", "your combined text is too short")
如果你想进行本地化,在视图中执行此操作会派上用场。
希望这有帮助!