title: MVC 3 Custom ValidationAttribute for CheckBoxFor firing twice on client-side
body: <p>I'm going to lay out the code to explain the whole situation then pose the description of the problem:</p>
  
  <p>In my View Model I have a boolean property to track whether the user has accepted terms:</p>
  
  <pre><code> [MustBeTrue(ErrorMessageResourceType = typeof(ErrorMessages), ErrorMessageResourceName = "MustAccept")]
   public bool HasAuthorizedBanking { get; set; }
  </code></pre>
  
  <p>As you can see I've created a custom validation attribute to handle this called MustBeTrue to handle the Checkbox, since [Required] is <a href="http://itmeze.com/2010/12/06/checkbox-has-to-be-checked-with-unobtrusive-jquery-validation-and-asp-net-mvc-3/" rel="noreferrer">currently not working for client-side validation on Checkboxes in MVC 3</a> </p>
  
  <pre><code>public class MustBeTrueAttribute : ValidationAttribute, IClientValidatable
  {
      protected override ValidationResult IsValid(object value, ValidationContext validationContext)
      {
          if ((bool)value)
              return ValidationResult.Success;
  
          return new ValidationResult(String.Format(ErrorMessageString,validationContext.DisplayName));
      }
  
      public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
      {
          var rule = new ModelClientValidationRule
          {
              ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
              ValidationType = "truerequired"
          };
  
          yield return rule;
      }
  }
  </code></pre>
  
  <p>Then I add a CheckBoxFor with a ValidationMessage to my View:</p>
  
  <pre><code>@Html.CheckBoxFor(model =&gt; model.HasAuthorizedBanking)
  @Html.ValidationMessageFor(model =&gt; model.HasAuthorizedBanking, "", new { @class = "validationtext" })  
  </code></pre>
  
  <p>In order to implement this client-side I created a jQuery validator method, and added an unobtrusive adapter:</p>
  
  <pre><code>// Checkbox Validation
  jQuery.validator.addMethod("checkrequired", function (value, element) {
      var checked = false;
      checked = $(element).is(':checked');
      return checked;
  }, '');
  
  jQuery.validator.unobtrusive.adapters.addBool("truerequired", "checkrequired");
  </code></pre>
  
  <p>In my view all the steps in the signup process are on one page, the elements are hidden and shown via jQuery and validated using jQuery Validation. When the 'Next' button is clicked each input element on the page is triggered for validation:</p>
  
  <pre><code> var validator = $("#WizardForm").validate(); // obtain validator
      var anyError = false;
      $step.find("input").each(function () {
          if (!validator.element(this)) { // validate every input element inside this step
              anyError = true;
          }
  
      });
  
      if (anyError)
          return false;
  </code></pre>
  
  <p><strong>Notes:</strong> </p>
  
  <ul>
  <li>There is only one property in my model with the MustBeTrue attribute, and there is only one CheckBoxFor &amp; matching ValidationMessageFor on the entire page.</li>
  <li>In order to track when this method is being called I simply put an alert(checked); inside the jQuery Validator method 'checkrequired'.</li>
  </ul>
  
  <p><strong>The problem:</strong> When the checkbox is checked/unchecked the 'checkrequired' method is fired once. However, when the 'Next' button is clicked and we set off to validate all the input elements, it is fired twice whether or not the checkbox is checked. <em>Interestingly</em>, if it is checked, the first validation returns true and the second returns false (this second false return is my main problem - the page will not validate and it will not allow you to continue to the next step). Additionally, when it is checked and Next is clicked - the ValidationMessageFor message disappears as though it is valid.</p>
  
  <p><strong>Edit:</strong>  I have another Custom Attribute for validating Age in a jQuery DatePicker textbox, and while it is implemented this exact same way - it only fires once under the same conditions.</p>