Question

I am coding a custom validation to my .NET MVC 4 application. This is the first validation that uses a parameter, and I'm finding some trouble to get this.

This is my C# code:

ValidationAttribute:

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;
        }
    }
}

ModelClientValidationRule:

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

And here is the JS code, my problem:

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;
});

Many thanks for the help!

Was it helpful?

Solution

see if this link helps:

ASP.NET MVC 3 client-side validation with parameters

Also, try changing your javascript code to this:

$.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;
});

The secret here is to use [ 'minlength' ] instead of {} It took me a lot of time when I first learnt this. I hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top