0

我的页面有一个业务规则 - 日期控件应该按升序排列。即输入到 Date1 的值 < 输入到 Date2 的值 < Date3

我的属性:

public class CompareDateAttribute : ValidationAttribute, IClientValidatable
{
    public CompareDateAttribute(string datesString)
    {
        _datesString = datesString;
    }
    private string _datesString;

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule()
        {
            //ErrorMessage = this.FormatErrorMessage(metadata.DisplayName),
            ErrorMessage = String.Format("Invalid date {0}", metadata.PropertyName),
            ValidationType = "datecomparer"
        };
        rule.ValidationParameters.Add("dates", _datesString);
        yield return rule;
    }
}

客户端:

$.validator.addMethod("datecomparer", function (value, element, param) {
    //var valid = true;
    var arrElements = param.split(',');
    var datePreviousStr = null;
    $.each(arrElements, function (index, v) {
        if (datePreviousStr == null)
        {
            datePreviousStr = $('#' + v).val();
        }
        else
        {
            var dateCurrentStr = $('#' + v).val();
            if (dateCurrentStr == '')
            {
                return false;
            }

            var dateCurrent = $.datepicker.parseDate("mm/dd/yy", dateCurrentStr);
            var datePrevious = $.datepicker.parseDate("mm/dd/yy", datePreviousStr);

            if (dateCurrent < datePrevious)
            {
                return false;
            }
            else
            {
                datePreviousStr = dateCurrentStr;
            }

        }
    });

    return true;
});

$.validator.unobtrusive.adapters.addSingleVal("datecomparer", "dates");

模型:

public class Valid6
{
    [CompareDate("Date1,Date2,Date3")]
    public DateTime Date1 { get; set; }
    [CompareDate("Date1,Date2,Date3")]
    public DateTime Date2 { get; set; }
    [CompareDate("Date1,Date2,Date3")]
    public DateTime Date3 { get; set; }
}

并查看(必要部分):

<div class="form-horizontal">
    <h4>Valid6</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Date1, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Date1, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Date1, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Date2, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Date2, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Date2, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Date3, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Date3, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Date3, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

此外,结果 HTML 的一部分:

<input class="form-control text-box single-line valid" data-val="true" data-val-date="The field Date3 must be a date." data-val-datecomparer="Invalid date Date3" data-val-datecomparer-dates="Date1,Date2,Date3" data-val-required="The Date3 field is required." id="Date3" name="Date3" type="datetime" value="">

但是当我输入 Date1='11/11/2010' 和 Date2='11/11/2009' 调试器说,datecomparer 方法返回 false,但没有出现验证器消息。为什么以及如何解决?

4

1 回答 1

0

找到了解决办法——“return false;” 内部循环仅从循环返回,然后在方法结束时找到“返回真”。

应该是这样的:

$.validator.addMethod("datecomparer", function (value, element, param) {
    var valid = true;
    var arrElements = param.split(',');
    var datePreviousStr = null;
    $.each(arrElements, function (index, v) {
        if (datePreviousStr == null)
        {
            datePreviousStr = $('#' + v).val();
        }
        else
        {
            var dateCurrentStr = $('#' + v).val();
            if (dateCurrentStr == '')
            {
                valid = false;
                return;
            }

            var dateCurrent = $.datepicker.parseDate("mm/dd/yy", dateCurrentStr);
            var datePrevious = $.datepicker.parseDate("mm/dd/yy", datePreviousStr);

            if (dateCurrent < datePrevious)
            {
                valid = false;
                return;
            }
            else
            {
                datePreviousStr = dateCurrentStr;
            }

        }
    });

    return valid;
});

$.validator.unobtrusive.adapters.addSingleVal("datecomparer", "dates");
于 2015-03-20T23:55:36.323 回答