使用远程验证进行多个字段验证

时间:2012-04-15 16:11:28

标签: c# asp.net-mvc asp.net-mvc-3 remote-validation

我有以下型号:

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 个答案:

答案 0 :(得分:22)

扩展了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"));
});

答案 1 :(得分:5)

我的工作方式是添加一些JavaScript。

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

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

答案 2 :(得分: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中验证所有字段是否已填写

答案 3 :(得分: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();
            });
        });
    });
}