title: MVC3 custom unobtrusive validation - force validation from checkbox
body: <p>I have the following class used for custom validation:</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Property, AllowMultiple=false, Inherited=true)]
  public sealed class RequiredIfAnyTrueAttribute : ValidationAttribute, IClientValidatable
  {
      private const string DefaultErrorMessage = "{0} is required";
  
      public List&lt;string&gt; OtherProperties { get; private set; }
  
      public RequiredIfAnyTrueAttribute(string otherProperties)
          : base(DefaultErrorMessage)
      {
          if (string.IsNullOrEmpty(otherProperties))
              throw new ArgumentNullException("otherProperty");
  
          OtherProperties = new List&lt;string&gt;(otherProperties.Split(new char[] { '|', ',' }));
      }
  
      public override string FormatErrorMessage(string name)
      {
          return string.Format(ErrorMessageString, name);
      }
  
      protected override ValidationResult IsValid(object value, ValidationContext validationContext)
      {
          if (value == null)
          {
              foreach (string s in OtherProperties)
              {
                  var otherProperty = validationContext.ObjectType.GetProperty(s);
                  var otherPropertyValue = otherProperty.GetValue(validationContext.ObjectInstance, null);
  
                  if (otherPropertyValue.Equals(true))
                      return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
              }
          }
  
          return ValidationResult.Success;
      }
  
      public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
      {
          var clientValidationRule = new ModelClientValidationRule()
          {
              ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
              ValidationType = "requiredifanytrue"
          };
  
          clientValidationRule.ValidationParameters.Add("otherproperties", string.Join("|",OtherProperties));
  
          return new[] { clientValidationRule };
      }
  }
  </code></pre>
  
  <p>My ViewModel is:</p>
  
  <pre><code>public class SampleViewModel
  {
      public bool PropABC { get; set; }
      public bool PropXYZ { get; set; }
  
      [RequiredIfAnyTrue("PropABC|PropXYZ")]
      public int? TestField { get; set; }
  }
  </code></pre>
  
  <p>When my strongly typed view renders, everything sees to work fine.  If PropABC or PropXYZ is selected then I am required to enter a value for TestField.  Both client and server-side validation is functional.</p>
  
  <p>However, given the following sequence of events:</p>
  
  <ol>
  <li>check PropABC</li>
  <li>submit form</li>
  <li>client-side validation fires for TestField required</li>
  <li>uncheck PropABC</li>
  <li>client validation does not re-fire and validation message
  remains until form submit</li>
  </ol>
  
  <p>In order to resolve #5 I would typically attach click events to the checkboxes via jquery onready to refire the validation.</p>
  
  <p><strong>Is there a preferred/recommended way to manually force client-side validation given MVC3 + unobstrusive + jquery?</strong>  </p>