1
我想在我的mvc應用程序上使用不顯眼的驗證。 我已經創建類MVC不顯眼驗證
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
公共密封類RequiredIfAttribute:ValidationAttribute,IClientValidatable { 私人常量字符串_defaultErrorMessage = 「{0}是必需的」;
private string _targetPropertyName;
private bool _targetPropertyCondition;
public RequiredIfAttribute(string targetPropertyName, bool targetPropertyCondition)
: base(_defaultErrorMessage)
{
this._targetPropertyName = targetPropertyName;
this._targetPropertyCondition = targetPropertyCondition;
}
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, name, _targetPropertyName, _targetPropertyCondition);
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
bool result = false;
bool propertyRequired = false;
var targetProperty = validationContext.ObjectType.GetProperty(_targetPropertyName);
var targetPropertyValue = (bool)targetProperty.GetValue(validationContext.ObjectInstance, null);
if (targetPropertyValue == _targetPropertyCondition)
{
propertyRequired = true;
}
if (propertyRequired)
{
if (value == null)
{
var message = FormatErrorMessage(validationContext.DisplayName);
return new ValidationResult(message);
}
}
return null;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule();
rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
rule.ValidationType = "requiredif";
rule.ValidationParameters.Add("targetpropertyname", this._targetPropertyName);
rule.ValidationParameters.Add("targetpropertyvalue", this._targetPropertyCondition.ToString().ToLower());
yield return rule;
}
}
我對客戶
$(function() {
$.validator.addMethod("requiredif", function (value, element, param) {
if ($(param.propertyname).is(':checked').toString() == param.propertyvalue) {
if (!this.depend(param, element))
return "dependency-mismatch";
switch (element.nodeName.toLowerCase()) {
case 'select':
var val = $(element).val();
return val && val.length > 0;
case 'input':
if (this.checkable(element))
return this.getLength(value, element) > 0;
default:
return $.trim(value).length > 0;
}
}
return true;
});
$.validator.unobtrusive.adapters.add("requiredif", ["targetpropertyname", "targetpropertyvalue"], function (options) {
if (options.element.tagName.toUpperCase() !== "INPUT" || options.element.type.toUpperCase() !== "CHECKBOX") {
options.rules["requiredif"] = {
propertyname: "#" + options.params.targetpropertyname,
propertyvalue: options.params.targetpropertyvalue
};
options.messages["requiredif"] = options.message;
}
});
}(jQuery的))驗證函數;
但這不驗證。爲什麼?謝謝
由於使用正確的屬性!那! – 2012-02-20 10:55:53