2

我正在为我的 .NET MVC 4 应用程序编写自定义验证。这是第一个使用参数的验证,我发现了一些麻烦。

这是我的 C# 代码:

验证属性:

namespace Validations
{
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class MinRequiredPasswordLengthValidationAttribute : ValidationAttribute, IClientValidatable
    {
        public MinRequiredPasswordLengthValidationAttribute()
        : base("The password is invalid")
        {

        }

        public override string FormatErrorMessage(string name)
        {
            return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name.ToLower(), Membership.MinRequiredPasswordLength);
        }

        public override bool IsValid(object value)
        {
            if (value == null || String.IsNullOrEmpty(value.ToString()))
            {
                return true;
            }

            return value.ToString().Length >= Membership.MinRequiredPasswordLength;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            ModelClientMinRequiredPasswordLengthValidationRule rule = new ModelClientMinRequiredPasswordLengthValidationRule(FormatErrorMessage(metadata.GetDisplayName()), Membership.MinRequiredPasswordLength);
            yield return rule;
        }
    }
}

模型客户端验证规则:

namespace Validations
{
    public class ModelClientMinRequiredPasswordLengthValidationRule : ModelClientValidationRule
    {
        public ModelClientMinRequiredPasswordLengthValidationRule(string errorMessage, int minRequiredPasswordLength)
        {
            ErrorMessage = errorMessage;
            ValidationType = "minrequiredpasswordlength";
            ValidationParameters.Add("minlength", minRequiredPasswordLength);
        }
    }
}

这是JS代码,我的问题:

jQuery.validator.addMethod("minrequiredpasswordlength", function (value, element, params) {
    if (value.length == 0) {
        return true; //empty
}

return true; //dummy return
}, "");

jQuery.validator.unobtrusive.adapters.add("minrequiredpasswordlength", {}, function (options) {
    //XXX Here I should get the minlength parameter, but 'options' is a empty object
    options.messages["minrequiredpasswordlength"] = options.message;
});

非常感谢您的帮助!

4

1 回答 1

1

看看这个链接是否有帮助:

带有参数的 ASP.NET MVC 3 客户端验证

另外,尝试将您的 javascript 代码更改为:

$.validator.addMethod('minrequiredpasswordlength', function (value, element, params) {
    var minLength = params.minLength;

    // minLength should contain your value.
    return true; // dummy return
});

jQuery.validator.unobtrusive.adapters.add('minrequiredpasswordlength', [ 'minlength' ], function (options) {

    options.rules['minrequiredpasswordlength'] = {
            minLength: options.params['minlength']
        };

    //XXX Here I should get the minlength parameter, but 'options' is a empty object
    options.messages["minrequiredpasswordlength"] = options.message;
});

这里的秘诀是使用 [ 'minlength' ] 而不是 {} 我第一次学习这个时花了很多时间。我希望这有帮助。

于 2014-01-16T13:13:01.900 回答