Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC unobtrusive validation on checkbox not working
    primarykey
    data
    text
    <p>I'm trying to implement the code as mentioned in <a href="https://stackoverflow.com/questions/2245185/how-to-handle-booleans-checkboxes-in-asp-net-mvc-2-with-dataannotations">this post</a>. In other words I'm trying to implement unobtrusive validation on a terms and conditions checkbox. If the user hasn't selected the checkbox, then the input should be marked as invalid. </p> <p>This is the server side Validator code, I've added:</p> <pre><code>/// &lt;summary&gt; /// Validation attribute that demands that a boolean value must be true. /// &lt;/summary&gt; [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)] public class MustBeTrueAttribute : ValidationAttribute { public override bool IsValid(object value) { return value != null &amp;&amp; value is bool &amp;&amp; (bool)value; } } </code></pre> <p>This is the model</p> <pre><code>[MustBeTrue(ErrorMessage = "You must accept the terms and conditions")] [DisplayName("Accept terms and conditions")] public bool AcceptsTerms { get; set; } </code></pre> <p>This is my view:</p> <pre><code>@Html.EditorFor(x =&gt; x.AcceptTermsAndConditions) @Html.LabelFor(x =&gt; x.AcceptTermsAndConditions) @Html.ValidationMessageFor(x =&gt; x.AcceptTermsAndConditions) </code></pre> <p>and this is the jQuery I've used to attach the validator client side:</p> <pre><code>$.validator.unobtrusive.adapters.addBool("mustbetrue", "required"); </code></pre> <p>The client side script doesn't appear to be kicking in, however. Whenever I press the submit button, validation on the other fields kicks in fine, but the validation for the Terms &amp; conditions doesn't seem to kick in. This is how the code looks in Firebug after I've clicked the submit button.</p> <pre><code>&lt;input type="checkbox" value="true" name="AcceptTermsAndConditions" id="AcceptTermsAndConditions" data-val-required="The I confirm that I am authorised to join this website and I accept the terms and conditions field is required." data-val="true" class="check-box"&gt; &lt;input type="hidden" value="false" name="AcceptTermsAndConditions"&gt; &lt;label for="AcceptTermsAndConditions"&gt;I confirm that I am authorised to join this website and I accept the terms and conditions&lt;/label&gt; &lt;span data-valmsg-replace="true" data-valmsg-for="AcceptTermsAndConditions" class="field-validation-valid"&gt;&lt;/span&gt; </code></pre> <p>Any ideas? Have I missed out a step? This is driving me potty!</p> <p>Thanks in advance S</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.
 

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