我很难用jQuery不引人注目的验证来掌握MVC3。
我有一个表单,我需要用户在发出POST
请求之前至少输入一个字段。
我非常关注Darin Dimitrov's Answer Here,但如果没有字段值,我似乎无法弄清楚我需要做什么来阻止表单提交。
自定义属性:
Public Class AtLeastOneRequiredAttribute
Inherits ValidationAttribute
Implements IClientValidatable
Private ReadOnly _properties As String()
Public Sub New(ByVal properties As String())
_properties = properties
End Sub
Protected Overrides Function IsValid(value As Object, validationContext As ValidationContext) As ValidationResult
If IsNothing(_properties) Or _properties.Length < 1 Then
Return Nothing
End If
For Each prop In _properties
Dim propertyInfo = validationContext.ObjectType.GetProperty(prop)
If IsNothing(propertyInfo) Then
Return New ValidationResult(String.Format("unknown property {0}", prop))
End If
Dim propertyValue = propertyInfo.GetValue(validationContext.ObjectInstance, Nothing)
If TypeOf propertyValue Is String AndAlso Not String.IsNullOrEmpty(propertyValue.ToString) Then
Return Nothing
End If
If Not IsNothing(propertyValue) Then
Return Nothing
End If
Next
Return New ValidationResult(FormatErrorMessage(validationContext.DisplayName))
End Function
Public Function GetClientValidationRules(metadata As ModelMetadata, context As ControllerContext) As IEnumerable(Of ModelClientValidationRule) Implements IClientValidatable.GetClientValidationRules
Dim result = New List(Of ModelClientValidationRule)
Dim rule As New ModelClientValidationRule
rule.ErrorMessage = ErrorMessage
rule.ValidationType = "atleastonerequired"
rule.ValidationParameters("properties") = String.Join(",", _properties)
result.Add(rule)
Return result
End Function
End Class
型号:
<AtLeastOneRequired({"FieldA", "FieldB", "FieldC"}, ErrorMessage:="Testing")> _
Public Property FieldA As String
Public Property FieldB As String
Public Property FieldC As String
查看:
<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>
jQuery.validator.unobtrusive.adapters.add(
'atleastonerequired', ['properties'], function (options) {
options.rules['atleastonerequired'] = options.params;
options.messages['atleastonerequired'] = options.message;
}
);
jQuery.validator.addMethod('atleastonerequired', function (value, element, params) {
var properties = params.properties.split(',');
var values = $.map(properties, function (property, index) {
var val = $('#' + property).val();
return val != '' ? val : null;
});
return values.length > 0;
}, '');
@Using Html.BeginForm("Results", "List")
@Html.ValidationSummary(False)
@<div>
@Html.LabelFor(Function(model) model.FieldA)
@Html.EditorFor(Function(model) model.FieldA)
</div>
@<div>
@Html.LabelFor(Function(model) model.FieldB)
@Html.EditorFor(Function(model) model.FieldB)
</div>
@<div>
@Html.LabelFor(Function(model) model.FieldC)
@Html.EditorFor(Function(model) model.FieldC)
</div>
@<p>
<input type="submit" value="Search" />
</p>
End Using
答案 0 :(得分:1)
上面的代码实际上有效,但我尝试简化我的代码以进行演示,这就是错误存在的地方。经验教训。
实际查看代码:
@<div class="ui-widget">
@Html.LabelFor(Function(model) model.CategoryID)
<input class="text-box single-line" id="Category" name="Category" type="text" value="" />
</div>
@<div class="ui-widget">
@Html.LabelFor(Function(model) model.Manufacturer)
@Html.EditorFor(Function(model) model.Manufacturer)
</div>
@<div>
@Html.LabelFor(Function(model) model.aModel)
@Html.EditorFor(Function(model) model.aModel)
</div>
实际型号:
<Display(Name:="Category")> _
<AtLeastOneRequired({"CategoryID", "Manufacturer", "aModel"}, ErrorMessage:="Testing")> _
Public Property CategoryID As String
Public Property Manufacturer As String
<Display(Name:="Model")> _
Public Property aModel As String
早些时候我正在弄乱jQuery Autocomplete并手动设置文本框而不是使用HTML Helpers
。然后我将自定义属性分配给我的CategoryID
属性。当我将AtLeastOneRequried
属性移至另一个属性Manufacturer
或Model
时,它就有用了。
请记住使用HTML Helper将自定义属性绑定到属性,否则无法在源代码中正确呈现。