5

I'm running into a problem where I have a simple add/edit form and using client-side validation (jQuery/MicrosoftMvcJQueryValidation.js) which is generated from data annotations and enabled client side by calling the following in my view:

<% Html.EnableClientValidation(); %>

This seems to work fine for most elements, however I have an instance where I have a boolean property which is rendered as a checkbox using:

<%= Html.EditorFor(model => model.Chargeable)%>

Which can be either true/false (ticked/unticked).

As the bool is a value type, and not nullable, it is being rendered as a required property and displays an error (client side) when the form is submitted reading "The Chargeable field is required.", however, as the HTML that is generated is two part (both checkbox and hidden value) it will pass the post back validation.

After browsing the MVC 2 source code, I've managed to put a "quick and dirty" fix in for the moment, which is to set:

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

Any one else got any ideas or suggestions on how I can get around this?

IMO, I dont think MVC should be setting the client-side required validator for checkboxes rendered using the Html.EditorFor/Html.CheckBox(For) methods.

Note: I'm using the ASP.NET MVC 2 RC2 and the MicrosoftMvcJQueryValidation.js from the matching MVC Futures release.

1 Answer 1

6

I suppose the easiest way of handling it is to call the rules("remove", [rules]) function on the elements (mainly checkboxes) that I want to remove the client-side validation from:

<script type="text/javascript">
  $(document).ready(function() {
    $('#Chargeable').rules('remove', 'required');
  });
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you saved me, although there was no rules function and I used $('#Chargeable').removeAttr('required');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.