16

我有以下模型:

public class Customer
{
    public string FirstName {get;set;}

    public string LastName {get; set;}

    [Remote("CardExisting", "Validation", AdditionalFields="FirstName,LastName")
    public string CardNumber {get; set;}
}

CardExisting 操作将检查 firstName 和 LastName 的 cardNumber 组合是否存在现有记录。

如果用户先输入卡号,然后输入他的姓名,我无法验证他,所以当他返回并输入他的姓名时,我需要再次远程验证,当焦点已经从 cardnumber 属性中丢失时,我该怎么办?

4

4 回答 4

23

扩展 Jaluka 的答案,我编写了这个帮助方法,它找到每个具有“附加字段”的远程验证元素,然后在每次这些字段之一发生变化时触发对所述元素的验证。

// I hate naming things
function initializeRemotelyValidatingElementsWithAdditionalFields($form) {
    var remotelyValidatingElements = $form.find("[data-val-remote]");

    $.each(remotelyValidatingElements, function (i, element) {
        var $element = $(element);

        var additionalFields = $element.attr("data-val-remote-additionalfields");

        if (additionalFields.length == 0) return;

        var rawFieldNames = additionalFields.split(",");

        var fieldNames = $.map(rawFieldNames, function (fieldName) { return fieldName.replace("*.", ""); });

        $.each(fieldNames, function (i, fieldName) {
            $form.find("#" + fieldName).change(function () {
                // force re-validation to occur
                $element.removeData("previousValue");
                $element.valid();
            });
        });
    });
}

像这样调用函数:

$(document).ready(function() {
    initializeRemotelyValidatingElementsWithAdditionalFields($("#myFormId"));
});
于 2013-07-19T13:52:31.043 回答
6

我得到这个工作的方法是添加一些 JavaScript。

$("#FirstName").change(function () {
  $('#CardNumber').removeData('previousValue');
  $('#CardNumber').valid();
});

因此,当更改名字时,您会清除卡号中的所有先前值并重新验证卡号。

于 2012-04-17T04:11:03.573 回答
4

如果您不喜欢创建客户端脚本:

    public class Customer
    {
        [Remote("CardExisting", "Validation", AdditionalFields = "CardNumber,LastName")]
        public string FirstName { get; set; }
        [Remote("CardExisting", "Validation", AdditionalFields = "FirstName,CardNumber")]
        public string LastName { get; set; }
        [Remote("CardExisting", "Validation", AdditionalFields = "FirstName,LastName")]
        public string CardNumber { get; set; }
    }

并在 CardExisting 中验证所有字段是否已填写

于 2015-03-15T20:39:18.250 回答
0

继上面 Chris 的辅助方法之后。这在大多数情况下都很好用,但如果您的模型完全有前缀,例如 'Custom.Address.Line1' 。未正确找到附加字段。我已经使用 jquery.validate.unobstructive 库中的一些附加逻辑更新了帮助程序,这些逻辑将正确查找字段。

function getModelPrefix(fieldName) {
    return fieldName.substr(0, fieldName.lastIndexOf(".") + 1);
}

function appendModelPrefix(value, prefix) {
    if (value.indexOf("*.") === 0) {
        value = value.replace("*.", prefix);
    }
    return value;
}

function escapeAttributeValue(value) {
    // As mentioned on http://api.jquery.com/category/selectors/
    return value.replace(/([!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~])/g, "\\$1");
}

function initializeRemotelyValidatingElementsWithAdditionalFields($form) {
    const remotelyValidatingElements = $form.find("[data-val-remote]");

    $.each(remotelyValidatingElements, function (i, element) {
        var $element = $(element);

        const additionalFields = $element.attr("data-val-remote-additionalfields");

        if (additionalFields.length === 0) return;

        const rawFieldNames = additionalFields.split(",");
        const prefix = getModelPrefix(element.name);

        const fieldNames = $.map(rawFieldNames, function(fieldName) {
            return appendModelPrefix(fieldName, prefix);
        });

        $.each(fieldNames, function (i, fieldName) {
            $form.find(":input").filter("[name='" + escapeAttributeValue(fieldName) + "']").change(function () {
                // force re-validation to occur
                $element.removeData("previousValue");
                $element.valid();
            });
        });
    });
}
于 2018-10-16T10:04:02.823 回答