0

我试图从自定义数据属性 JQuery 不显眼的适配器中获取错误消息,但我似乎无法将错误消息提取到变量中。我的验证消息返回为:

警告:没有为图像定义消息

代码如下:

$(document).ready(function () {
var errorMessage;
$.validator.unobtrusive.adapters.add(
    'filesize', ['maxsize'], function (options) {
        options.rules['filesize'] = options.params;

        if (options.message) {
            options.message['filesize'] = options.message;             

            $.each(options, function (key, val) {
                console.log("Key: " + key + " | Value: " + val);
                if (key === "message") {
                    errorMessage = val;
                }
            });

        }
    });

$.validator.addMethod('filesize', function (value, element, params) {
    if (element.files.length < 1) {
        // No files selected
        return true;
    }

    if (!element.files || !element.files[0].size) {
        // This browser doesn't support the HTML5 API
        return true;
    }

    return element.files[0].size < params.maxsize;
}, errorMessage); // This is where the variable errorMessage is used
});

此外,当我使用 options.messages(复数)的正确 JQuery 语法并省略整个 $.each 块时,我的 Firefox 在我打开 Firebug 时崩溃?

4

2 回答 2

1

您通常会在您在视图模型上编写的自定义验证属性中指定错误消息:

public class MyViewModel
{
    [Required]
    [MaxFileSize(8388608, ErrorMessage = "Maximum allowed file size is {0} bytes")]
    public HttpPostedFileBase File { get; set; }
}

然后有一个自定义验证属性:

public class MaxFileSizeAttribute : ValidationAttribute, IClientValidatable
{
    private readonly int _maxFileSize;
    public MaxFileSizeAttribute(int maxFileSize)
    {
        _maxFileSize = maxFileSize;
    }

    public override bool IsValid(object value)
    {
        var file = value as HttpPostedFileBase;
        if (file == null)
        {
            return false;
        }
        return file.ContentLength <= _maxFileSize;
    }

    public override string FormatErrorMessage(string name)
    {
        return base.FormatErrorMessage(_maxFileSize.ToString());
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = FormatErrorMessage(_maxFileSize.ToString()),
            ValidationType = "filesize"
        };
        rule.ValidationParameters["maxsize"] = _maxFileSize;
        yield return rule;
    }
}

和不显眼的适配器:

jQuery.validator.unobtrusive.adapters.add(
    'filesize', [ 'maxsize' ], function (options) {
        options.rules['filesize'] = options.params;
        if (options.message) {
            options.messages['filesize'] = options.message;
        }
    }
);

jQuery.validator.addMethod('filesize', function (value, element, params) {
    if (element.files.length < 1) {
        // No files selected
        return true;
    }

    if (!element.files || !element.files[0].size) {
        // This browser doesn't support the HTML5 API
        return true;
    }

    return element.files[0].size < params.maxsize;
}, '');
于 2012-08-15T17:20:32.920 回答
0

要将键与字符串进行比较 $.each 你可以简单地做

var myObj = {'message1' : 1, 'message2' : 2};
$.each(​myObj, function(key){
     if(key == 'message1'){
         alert('they match!');                     
     }                                
});​
于 2012-08-15T17:26:08.297 回答