Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC2 Custom jQuery validation: client -side
    primarykey
    data
    text
    <p>I want to create a validation rule for 2 date-pickers (startDate less then endDate).</p> <p>I create a validation attribute:</p> <pre><code>[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?) &amp;&amp; endValue.GetType() == typeof(DateTime?)) { var start = ((DateTime?)startValue); var end = ((DateTime?)endValue); return (start.Value &lt; end.Value); } return false; } } </code></pre> <p>and added ti to my Dto:</p> <pre><code>[DateCompare("StartDate", "EndDate")] public class QualificationInput{...} </code></pre> <p>I created a validator:</p> <pre><code>public class DateCompareValidator : DataAnnotationsModelValidator&lt;DateCompareAttribute&gt; { 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&lt;ModelClientValidationRule&gt; GetClientValidationRules() { var rule = new ModelClientValidationRule { ErrorMessage = _message, ValidationType = "dateCompare" }; rule.ValidationParameters.Add("startField", startField); rule.ValidationParameters.Add("endField", endField); return new[] { rule }; } } </code></pre> <p>And registered it in Global.asax.cs in Application_Start():</p> <pre><code>DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(DateCompareAttribute), typeof(DateCompareValidator)); </code></pre> <p>In MicrosoftMvcJQueryValidation.js I have made this changes:</p> <pre><code>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() &lt; $('#' + params["endField"]).val()) { return true; } return false; }, jQuery.format("Error")); </code></pre> <p>But it doesn't work :( no client side validation on this type of rule (the others type like required works fine)</p> <p>What I'm doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload