Asp.net MVC2 Custom jquery validation: client -side

Posted by Lullaby on Stack Overflow See other posts from Stack Overflow or by Lullaby
Published on 2010-05-20T09:15:09Z Indexed on 2010/05/21 14:20 UTC
Read the original article Hit count: 828

Filed under:
|
|

Hi. I want to create a validation rule for 2 date-pickers (startDate less then endDate).

I create a validation attribute:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] public sealed class DateCompareAttribute : ValidationAttribute { private const string _defaultErrorMessage = "'{0}' is less then '{1}'.";

    public DateCompareAttribute(string startDateProperty, string endDateProperty)
        : base(_defaultErrorMessage)
    {
        StartDateProperty = startDateProperty;
        EndDateProperty = endDateProperty;
    }

    public string StartDateProperty { get; private set; }
    public string EndDateProperty { get; private set; }


    public override string FormatErrorMessage(string name)
    {
        return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
                             StartDateProperty, EndDateProperty);
    }

    public override bool IsValid(object value)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
        object startValue = properties.Find(StartDateProperty, true).GetValue(value);
        object endValue = properties.Find(EndDateProperty, true).GetValue(value);
        if (startValue.GetType() == typeof(DateTime?) && endValue.GetType() == typeof(DateTime?))
        {
            var start = ((DateTime?)startValue);
            var end = ((DateTime?)endValue);
            return (start.Value < end.Value);
        }
        return false;
    }
}

and added ti to my Dto:

[DateCompare("StartDate", "EndDate")] public class QualificationInput{...}

I created a validator:

public class DateCompareValidator : DataAnnotationsModelValidator { string startField; private string endField; string _message;

    public DateCompareValidator(ModelMetadata metadata, ControllerContext context
      , DateCompareAttribute attribute)
        : base(metadata, context, attribute)
    {
        startField = attribute.StartDateProperty;
        endField = attribute.EndDateProperty;
        _message = attribute.ErrorMessage;
    }

    public override IEnumerable<ModelClientValidationRule>
     GetClientValidationRules()
    {
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = _message,
            ValidationType = "dateCompare"
        };
        rule.ValidationParameters.Add("startField", startField);
        rule.ValidationParameters.Add("endField", endField);

        return new[] { rule };
    }
}

And registered it in Global.asax.cs in Application_Start():

DataAnnotationsModelValidatorProvider .RegisterAdapter(typeof(DateCompareAttribute), typeof(DateCompareValidator));

In MicrosoftMvcJQueryValidation.js I have made this changes:

switch (thisRule.ValidationType) { .....

        case "dateCompare":
            __MVC_ApplyValidator_DateCompare(rulesObj,
                thisRule.ValidationParameters["startField"], thisRule.ValidationParameters["endField"]);
            break;

..... }

....

function __MVC_ApplyValidator_DateCompare(object, startField, endField) { object["startField"] = startField; object["endField"] = endField; }

jQuery.validator.addMethod("dateCompare", function(value, element, params) { if ($('#' + params["startField"]).val() < $('#' + params["endField"]).val()) { return true; } return false; }, jQuery.format("Error"));

But it doesn't work :( no client side validation on this type of rule (the others type like required works fine)

What I'm doing wrong?

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about validation