1

我有一個自定義的服務器端驗證程序,但我努力讓客戶端工作。這是我到目前爲止。自定義驗證程序的客戶端驗證asp.net MVC 4

確認者正在檢查進入一個值是至少進入另一個值的百分比。

public sealed class PercentageOfAttribute : ValidationAttribute, IClientValidatable 
{ 
    private readonly string sourcePropertyName; 
    private readonly int minimumPercentage; 

    public PercentageOfAttribute(string sourcePropertyName, int minimumPercentage) 
    { 
     this.sourcePropertyName = sourcePropertyName; 
     this.minimumPercentage = minimumPercentage; 
    } 

    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 
     var propertyName = validationContext.ObjectType.GetProperty(this.sourcePropertyName); 
     if (propertyName == null) 
      return new ValidationResult(String.Format("Uknown property {0}", this.sourcePropertyName)); 

     int propertyValue = (int)propertyName.GetValue(validationContext.ObjectInstance, null); 

     var minValue = (propertyValue/100) * this.minimumPercentage; 
     if ((int)value > minValue) 
      return ValidationResult.Success; 

     return new ValidationResult(FormatErrorMessage(validationContext.DisplayName)); 

    } 

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) 
    { 
     var rule = new ModelClientValidationRule 
     { 
      ErrorMessage = this.ErrorMessageString, 
      ValidationType = "percentageof" 
     }; 
     rule.ValidationParameters["propertyname"] = this.sourcePropertyName; 
     rule.ValidationParameters["minimumpercentage"] = this.minimumPercentage; 
     yield return rule; 
    } 
} 

而對於客戶端的JavaScript ...

jQuery.validator.addMethod("percentageof", function (value, element, params) { 
    var propertyname = params['propertyname']; 
    var minimumpercentage = param['minimumpercentage']; 
    var propValue = $('#' + propertyname).val(); 
    var minValue = (propValue/100) * minimumpercentage; 
    if (value >= minValue) { 
     return true; 
    } 
    return false; 
}); 
jQuery.validator.unobtrusive.adapters.add("percentageof", ["propertyname", "minimumpercentage"], function (options) { 
    options.rules['propertyname'] = options.params.propertyname; 
    options.rules['minimumpercentage'] = options.params.minimumpercentage; 
    options.message['percentageof'] = options.message; 
}); 

我已經調試的addapters.add功能,這是傳遞正確的數據。 「data-val-percentageof- *」值出現在html中並具有正確的值。

的JavaScript錯誤我目前得到的是「$ .validator.methods [方法]是不確定的」。我認爲這意味着我沒有正確設置客戶端驗證器。我已經閱讀了無數的例子和教程,但似乎無法找出一個有效的組合。

如果我刪除所有我的自定義的東西,那麼默認的客戶端驗證完美。

任何人都可以幫助告訴我我做錯了什麼嗎?

編輯:

呈現的HTML如下:

<div class="form-group"> 
<label class="col-sm-3 control-label" for="PurchasePrice">Purchase Price (£36,000 minimum) *</label> 
<div class="col-sm-6"> 
    <input id="PurchasePrice" class="form-control valid" type="text" value="0" name="PurchasePrice" data-val-required="Please enter a Purchase Price" data-val-range-min="36000" data-val-range-max="2147483647" data-val-range="Please enter at least £36,000" data-val-number="The field Purchase Price (£36,000 minimum) * must be a number." data-val="true"> 
    <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="PurchasePrice"></span> 
</div> 
</div> 
<div class="form-group"> 
<label class="col-sm-3 control-label" for="Deposit">Available Deposit (30% minimum)*</label> 
<div class="col-sm-6"> 
    <input id="Deposit" class="form-control" type="text" value="0" name="Deposit" data-val-required="Please enter an Available Deposit" data-val-percentageof-propertyname="PurchasePrice" data-val-percentageof-minimumpercentage="30" data-val-percentageof="Please enter value that is at least 30% of the purchase price" data-val-number="The field Available Deposit (30% minimum)* must be a number." data-val="true"> 
    <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="Deposit"></span> 
</div> 
</div> 
+0

由於這可能是一個JavaScript問題,顯示窗體的相關_rendered_ HTML標記將會很有幫助。 – Sparky

+0

感謝您的回覆 - 我在上面的編輯中添加了HTML。 – Snavebelac

回答

1

我設法改變... adapters.add函數的格式如下

jQuery.validator.addMethod("percentageof", function (value, element, params) { 
    var propertyname = params['propertyname']; 
    var minimumpercentage = params['minimumpercentage']; 
    var propValue = $('#' + propertyname).val(); 
    var minValue = (propValue/100) * minimumpercentage; 
    if (value >= minValue) { 
    return true; 
    } 
    return false; 
}); 
jQuery.validator.unobtrusive.adapters.add("percentageof", ["propertyname", "minimumpercentage"], function (options) { 
    options.rules["percentageof"] = options.params; 
    if (options.message) { 
    options.messages['percentageof'] = options.message; 
    } 
}); 
得到這個工作

我錯誤地試圖添加參數作爲規則!代碼的addMethod部分中還存在拼寫錯誤。