IDateErrorInfo
MVC 框架支持 IDateErrorInfo(可以在此处找到 Microsoft 教程)。默认模型绑定器将负责通过将 html 表单元素绑定到模型来重新创建模型对象。如果模型绑定器检测到模型实现了接口,那么它将使用接口方法来验证模型中的每个属性或验证整个模型。有关更多信息,请参阅教程。
如果您想使用此方法使用客户端验证,那么(引用 Steve Sanderson 的话)“利用其他验证规则的最直接方法是在视图中手动生成所需的属性”:
<p>
@Html.TextBoxFor(m.ClientName, new { data_val = "true", data_val_email = "Enter a valid email address", data_val_required = "Please enter your name"})
@Html.ValidationMessageFor(m => m.ClientName)
</p>
然后,这可用于触发已定义的任何客户端验证。有关如何定义客户端验证的示例,请参见下文。
显式验证
正如您所提到的,您可以在操作中明确验证模型。例如:
public ViewResult Register(MyModel theModel)
{
if (theModel.PropertyB < theModel.PropertyA)
ModelState.AddModelError("", "PropertyA must not be less then PropertyB");
if (ModelState.IsValid)
{
//save values
//go to next page
}
else
{
return View();
}
}
然后,您需要在视图@Html.ValidationSummary
中显示错误消息,因为上面的代码会添加模型级别错误而不是属性级别错误。
要指定属性级别错误,您可以编写:
ModelState.AddModelError("PropertyA", "PropertyA must not be less then PropertyB");
然后在视图中使用:
@Html.ValidationMessageFor(m => m.PropertyA);
显示错误消息。
同样,任何客户端验证都需要通过定义属性来手动链接视图中的客户端验证。
自定义模型验证属性
如果我正确理解了这个问题,那么您正在尝试验证一个模型,该模型包含一个值和一个集合,其中集合上的一个属性将被求和。
对于我将给出的示例,视图将向用户显示一个最大值字段和 5 个值字段。最大值字段将是模型中的单个值,其中 5 个值字段将成为集合的一部分。验证将确保值字段的总和不大于最大值字段。验证将被定义为模型上的一个属性,该属性也将很好地链接到 javascript 客户端验证。
风景:
@model MvcApplication1.Models.ValueModel
<h2>Person Ages</h2>
@using (@Html.BeginForm())
{
<p>Please enter the maximum total that will be allowed for all values</p>
@Html.EditorFor(m => m.MaximumTotalValueAllowed)
@Html.ValidationMessageFor(m => m.MaximumTotalValueAllowed)
int numberOfValues = 5;
<p>Please enter @numberOfValues different values.</p>
for (int i=0; i<numberOfValues; i++)
{
<p>@Html.EditorFor(m => m.Values[i])</p>
}
<input type="submit" value="submit"/>
}
我没有对值字段添加任何验证,因为我不想使示例过于复杂。
该模型:
public class ValueModel
{
[Required(ErrorMessage="Please enter the maximum total value")]
[Numeric] //using DataAnnotationExtensions
[ValuesMustNotExceedTotal]
public string MaximumTotalValueAllowed { get; set; }
public List<string> Values { get; set; }
}
行动:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(ValueModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
else
{
return RedirectToAction("complete"); //or whatever action you wish to define.
}
}
自定义属性:
[ValuesMustNotExceedTotal]
模型上定义的属性可以通过重写 ValidationAttribute 类来定义:
public class ValuesMustNotExceedTotalAttribute : ValidationAttribute
{
private int maxTotalValueAllowed;
private int valueTotal;
public ValuesMustNotExceedTotalAttribute()
{
ErrorMessage = "The total of all values ({0}) is greater than the maximum value of {1}";
}
public override string FormatErrorMessage(string name)
{
return string.Format(ErrorMessageString, valueTotal, maxTotalValueAllowed);
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
PropertyInfo maxTotalValueAllowedInfo = validationContext.ObjectType.GetProperty("MaximumTotalValueAllowed");
PropertyInfo valuesInfo = validationContext.ObjectType.GetProperty("Values");
if (maxTotalValueAllowedInfo == null || valuesInfo == null)
{
return new ValidationResult("MaximumTotalValueAllowed or Values is undefined in the model.");
}
var maxTotalValueAllowedPropertyValue = maxTotalValueAllowedInfo.GetValue(validationContext.ObjectInstance, null);
var valuesPropertyValue = valuesInfo.GetValue(validationContext.ObjectInstance, null);
if (maxTotalValueAllowedPropertyValue != null && valuesPropertyValue != null)
{
bool maxTotalValueParsed = Int32.TryParse(maxTotalValueAllowedPropertyValue.ToString(), out maxTotalValueAllowed);
int dummyValue;
valueTotal = ((List<string>)valuesPropertyValue).Sum(x => Int32.TryParse(x, out dummyValue) ? Int32.Parse(x) : 0);
if (maxTotalValueParsed && valueTotal > maxTotalValueAllowed)
{
return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
}
}
//if the maximum value is not supplied or could not be parsed then we still return that the validation was successful.
//why? because this attribute is only responsible for validating that the total of the values is less than the maximum.
//we use a [Required] attribute on the model to ensure that the field is required and a [Numeric] attribute
//on the model to ensure that the fields are input as numeric (supplying appropriate error messages for each).
return null;
}
}
将客户端验证添加到自定义属性:
要将客户端验证添加到此属性,它需要实现 IClientValidatable 接口:
public class ValuesMustNotExceedTotalAttribute : ValidationAttribute, IClientValidatable
{
//...code as above...
//this will be called when creating the form html to set the correct property values for the form elements
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule {
ValidationType = "valuesmustnotexceedtotal", //the name of the client side javascript validation (must be lowercase)
ErrorMessage = "The total of all values is greater than the maximum value." //I have provided an alternative error message as i'm not sure how you would alter the {0} and {1} in javascript.
};
yield return rule;
//note: if you set the validation type above to "required" or "email" then it would use the default javascript routines (by those names) to validate client side rather than the one we define
}
}
如果您此时要运行应用程序并查看定义属性的字段的源 html,您将看到以下内容:
<input class="text-box single-line" data-val="true" data-val-number="The MaximumTotalValueAllowed field is not a valid number." data-val-required="Please enter the maximum total value" data-val-valuesmustnotexceedtotal="The total of all values is greater than the maximum value." id="MaximumTotalValueAllowed" name="MaximumTotalValueAllowed" type="text" value="" />
特别注意data-val-valuesmustnotexceedtotal
. 这就是我们的客户端验证链接到验证属性的方式。
添加客户端验证:
要添加客户端验证,您需要在视图的标记中添加以下类似的库引用:
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
您还需要确保在 web.config 中打开客户端验证,尽管我认为这应该默认打开:
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
剩下的就是在视图中定义客户端验证。请注意,此处添加的验证是在视图中定义的,但如果它是在库中定义的,则可以将自定义属性(可能不是这个)添加到其他视图的其他模型中:
<script type="text/javascript">
jQuery.validator.unobtrusive.adapters.add('valuesmustnotexceedtotal', [], function (options) {
options.rules['valuesmustnotexceedtotal'] = '';
options.messages['valuesmustnotexceedtotal'] = options.message;
});
//note: this will only be fired when the user leaves the maximum value field or when the user clicks the submit button.
//i'm not sure how you would trigger the validation to fire if the user leaves the value fields although i'm sure its possible.
jQuery.validator.addMethod('valuesmustnotexceedtotal', function (value, element, params) {
sumValues = 0;
//determine if any of the value fields are present and calculate the sum of the fields
for (i = 0; i <= 4; i++) {
fieldValue = parseInt($('#Values_' + i + '_').val());
if (!isNaN(fieldValue)) {
sumValues = sumValues + fieldValue;
valueFound = true;
}
}
maximumValue = parseInt(value);
//(if value has been supplied and is numeric) and (any of the fields are present and are numeric)
if (!isNaN(maximumValue) && valueFound) {
//perform validation
if (sumValues > maximumValue)
{
return false;
}
}
return true;
}, '');
</script>
应该就是这样。我确信可以在这里和那里进行改进,如果我稍微误解了这个问题,你应该能够根据你的需要调整验证。但我相信这种验证似乎是大多数开发人员编写自定义属性的方式,包括更复杂的客户端验证。
希望这可以帮助。如果您对上述内容有任何问题或建议,请告诉我。