编辑:这假设你使用MVC 3.不幸的是我的代码是在VB.NET中,因为这是我在工作中使用。
为了使一切正常工作与新的不显眼的验证有一些事情你必须做。几周前我通过它们供电。
首先,创建一个继承自ValidationAttribute
的自定义属性类。一个简单的RequiredIf属性类如下:
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations
<AttributeUsage(AttributeTargets.Field Or AttributeTargets.Property, AllowMultiple:=False, Inherited:=False)> _
Public NotInheritable Class RequiredIfAttribute
Inherits ValidationAttribute
Private Const _defaultErrorMessage As String = "'{0}' is required."
Private ReadOnly _dependentProperty As String
Private ReadOnly _targetValues As Object()
Public Sub New(dependentProperty As String, targetValues As Object())
MyBase.New(_defaultErrorMessage)
_dependentProperty = dependentProperty
_targetValues = targetValues
End Sub
Public Sub New(dependentProperty As String, targetValues As Object(), errorMessage As String)
MyBase.New(errorMessage)
_dependentProperty = dependentProperty
_targetValues = targetValues
End Sub
Public ReadOnly Property DependentProperty() As String
Get
Return _dependentProperty
End Get
End Property
Public ReadOnly Property TargetValues() As Object()
Get
Return _targetValues
End Get
End Property
Public Overrides Function FormatErrorMessage(name As String) As String
Return String.Format(Globalization.CultureInfo.CurrentUICulture, ErrorMessageString, name)
End Function
Protected Overrides Function IsValid(value As Object, context As ValidationContext) As ValidationResult
' find the other property we need to compare with using reflection
Dim propertyValue = context.ObjectType.GetProperty(DependentProperty).GetValue(context.ObjectInstance, Nothing).ToString()
Dim match = TargetValues.SingleOrDefault(Function(t) t.ToString().ToLower() = propertyValue.ToLower())
If match IsNot Nothing AndAlso value Is Nothing Then
Return New ValidationResult(FormatErrorMessage(context.DisplayName))
End If
Return Nothing
End Function
End Class
接下来,您需要实现一个验证器类。这个类负责让MVC知道不引人注意的验证库工作所需的客户端验证规则。现在
Public Class RequiredIfValidator
Inherits DataAnnotationsModelValidator(Of RequiredIfAttribute)
Public Sub New(metaData As ModelMetadata, context As ControllerContext, attribute As RequiredIfAttribute)
MyBase.New(metaData, context, attribute)
End Sub
Public Overrides Function GetClientValidationRules() As IEnumerable(Of ModelClientValidationRule)
Dim rule As New ModelClientValidationRule() With {.ErrorMessage = ErrorMessage,
.ValidationType = "requiredif"}
rule.ValidationParameters("dependentproperty") = Attribute.DependentProperty.Replace("."c, HtmlHelper.IdAttributeDotReplacement)
Dim first As Boolean = True
Dim arrayString As New StringBuilder()
For Each param In Attribute.TargetValues
If first Then
first = False
Else
arrayString.Append(",")
End If
arrayString.Append(param.ToString())
Next
rule.ValidationParameters("targetvalues") = arrayString.ToString()
Return New ModelClientValidationRule() {rule}
End Function
End Class
你可以注册的Global.asax
应用启动方法的一切:
DataAnnotationsModelValidatorProvider.RegisterAdapter(GetType(RequiredIfAttribute), GetType(RequiredIfValidator))
到此你90%的方式出现。现在,你只需要告诉jQuery验证和MS的不显眼的验证层如何解读新属性:
/// <reference path="jquery-1.4.1-vsdoc.js" />
/// <reference path="jquery.validate-vsdoc.js" />
/* javascript for custom unobtrusive validation
==================================================== */
(function ($) {
// this adds the custom "requiredif" validator to the jQuery validate plugin
$.validator.addMethod('requiredif',
function (value, element, params) {
// the "value" variable must not be empty if the dependent value matches
// one of the target values
var dependentVal = $('#' + params['dependentProperty']).val().trim().toLowerCase();
var targetValues = params['targetValues'].split(',');
// loop through all target values
for (i = 0; i < targetValues.length; i++) {
if (dependentVal == targetValues[i].toLowerCase()) {
return $.trim(value).length > 0;
}
}
return true;
},
'not used');
// this tells the MS unobtrusive validation layer how to read the
// HTML 5 attributes that are output for the custom "requiredif" validator
$.validator.unobtrusive.adapters.add('requiredif', ['dependentProperty', 'targetValues'], function (options) {
options.rules['requiredif'] = options.params;
if (options.message) {
options.messages['requiredif'] = options.message;
}
});
} (jQuery));
希望这会有所帮助,这是一个真正的痛苦得到工作。
我不确定这一点,但我认为你几乎必须推出自己的客户端验证定制类似的东西。也许看看JQuery的验证-http://docs.jquery.com/Plugins/validation – Vishal 2011-02-09 23:04:44
你的目标是哪个版本的ASP.NET MVC? – 2011-02-09 23:04:58