0

我正在使用这个自定义验证。

public class DynamicRangeValidator : ValidationAttribute, IClientValidatable
{
    private readonly string _minPropertyName;
    private readonly string _maxPropertyName;

    public DynamicRangeValidator(string minPropertyName, string maxPropertyName)
    {
        _minPropertyName = minPropertyName;
        _maxPropertyName = maxPropertyName;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var minProperty = validationContext.ObjectType.GetProperty(_minPropertyName);
        var maxProperty = validationContext.ObjectType.GetProperty(_maxPropertyName);
        if (minProperty == null)
        {
            return new ValidationResult(string.Format("Unknown property {0}", _minPropertyName));
        }
        if (maxProperty == null)
        {
            return new ValidationResult(string.Format("Unknown property {0}", _maxPropertyName));
        }

        int minValue = (int)minProperty.GetValue(validationContext.ObjectInstance, null);
        int maxValue = (int)maxProperty.GetValue(validationContext.ObjectInstance, null);
        int currentValue = (int)value;
        if (currentValue <= minValue || currentValue >= maxValue)
        {
            return new ValidationResult(
                string.Format(
                    ErrorMessage,
                    minValue,
                    maxValue
                )
            );
        }

        return null;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule
        {
            ValidationType = "dynamicrange",
            ErrorMessage = this.ErrorMessage,
        };
        rule.ValidationParameters["minvalueproperty"] = _minPropertyName;
        rule.ValidationParameters["maxvalueproperty"] = _maxPropertyName;
        yield return rule;
    }
}

我的模型:

public class ProductModel{    
    public int MinValue{ get; set; }
    public int MaxValue{ get; set; }
    [DynamicRangeValidator("MinValue", "MaxValue", ErrorMessage = "Error Message")]
    public int Quantity{ get; set; }
}

看法

@model IEnumerable<ProductModel>
@using (Html.BeginForm(action, controller){
    @for (int i = 0; i < Model.Items.Count; i++){
         @Html.EditorFor(m => Model.Items[i].Quantity)
         @Html.ValidationMessageFor(m => Model.Items[i].Quantity)
         @Html.HiddenFor(m=>Model.Items[i].MinValue)
         @Html.HiddenFor(m=>Model.Items[i].MaxValue)
    }
<input type="submit">
}

JS:

$.validator.unobtrusive.adapters.add('dynamicrange', ['minvalueproperty', 'maxvalueproperty'],
        function (options) {
            options.rules['dynamicrange'] = options.params;
            if (options.message != null) {
                $.validator.messages.dynamicrange = options.message;
            }
        });

$.validator.addMethod('dynamicrange', function (value, element, params) {
    var minValue = parseInt($('input[name="' + params.minvalueproperty + '"]').val(), 10);
    var maxValue = parseInt($('input[name="' + params.maxvalueproperty + '"]').val(), 10);
    var currentValue = parseInt(value, 10);
    if (!isNaN(minValue) || !isNaN(maxValue) || !isNaN(currentValue) || minValue >= currentValue || currentValue >= maxValue) {
        $.validator.messages.dynamicrange = $.format($.validator.messages.dynamicrange, minValue, maxValue);
        return false;
    }
    return true;
}, '');

服务器端验证很好。客户端没有,因为我渲染的 html 是

<input class="text-box single-line input-validation-error" data-val="true"
data-val-dynamicrange="Error Message"
data-val-dynamicrange-maxvalueproperty="MaxValue"
data-val-dynamicrange-minvalueproperty="MinValue"
data-val-number="The field Quantita must be a number."
id="Items_4__Quantity" name="Items[4].Quantity"
type="number" value="0">

代替

<input class="text-box single-line input-validation-error" data-val="true"
data-val-dynamicrange="Error Message"
data-val-dynamicrange-maxvalueproperty="Items_4__.MaxValue"
data-val-dynamicrange-minvalueproperty="Items_4__.MinValue"
data-val-number="The field Quantita must be a number."
id="Items_4__Quantity" name="Items[4].Quantity"
type="number" value="0">

根据我的理解,我应该编辑 GetClientValidationRules 以添加前缀(集合名称和索引),但如何?

4

1 回答 1

1

好吧,我已经用一个小技巧以一种肮脏的方式解决了。

//dirty way
var rule = new ModelClientValidationRule
{
    ValidationType = "dynamicrange",
    ErrorMessage = this.ErrorMessage,
};
string Prefix = ((System.Web.Mvc.ViewContext)(context)).ViewData.TemplateInfo.HtmlFieldPrefix;
Prefix = Prefix.Replace(metadata.PropertyName, "");
System.Text.RegularExpressions.Regex Regex = new System.Text.RegularExpressions.Regex("[^a-zA-Z0-9 -]");
Prefix = Regex.Replace(Prefix, "_");
rule.ValidationParameters["minvalueproperty"] = Prefix + _minPropertyName;
rule.ValidationParameters["maxvalueproperty"] = Prefix + _maxPropertyName;
yield return rule;
于 2014-07-15T14:50:30.963 回答